admin管理员组

文章数量:1296844

I have a directory with some images in it that I want to make viewable in a browser.

This directory lives on my server at /public/images

Inside /public I also have other directories that I do not want to make public, hence making the entire /public directory viewable is not the solution.

How can I using the connect directory middleware make just my /public/images browsable?

Using the solution described here makes everything in /public viewable and trying the following doesn't work :

    app.use(exp.static(__dirname + '/public'));
    app.use(exp.static(__dirname + '/public/images'));
    app.use(exp.directory(__dirname + '/public/images'));

I have a directory with some images in it that I want to make viewable in a browser.

This directory lives on my server at /public/images

Inside /public I also have other directories that I do not want to make public, hence making the entire /public directory viewable is not the solution.

How can I using the connect directory middleware make just my /public/images browsable?

Using the solution described here makes everything in /public viewable and trying the following doesn't work :

    app.use(exp.static(__dirname + '/public'));
    app.use(exp.static(__dirname + '/public/images'));
    app.use(exp.directory(__dirname + '/public/images'));
Share Improve this question edited May 11, 2023 at 16:20 Rohad Bokhar 969 bronze badges asked Jul 16, 2012 at 17:44 braitschbraitsch 15.3k5 gold badges45 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

You have to mount your routes to a special path, like

app.use('/', exp.static(__dirname + '/public'));
app.use('/images', exp.static(__dirname + '/public/images'));
app.use('/images',exp.directory(__dirname + '/public/images'));

This way you can access the content of /public with the url / and the content of /public/image with the url /images

Express 4:

npm install serve-index

--

var serveIndex = require('serve-index');

app.use('/images', express.static(__dirname + '/public/images'), serveIndex(__dirname + '/public/images'));

本文标签: javascriptMaking a specific directory browsable in ExpressjsStack Overflow