简体   繁体   English

使Express.js中的特定目录可浏览

[英]Making a specific directory browsable in Express.js

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 该目录位于我的服务器上/ 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? 如何使用connect目录中间件使我的/ public / images可浏览?

Using the solution described here makes everything in /public viewable and trying the following doesn't work : 使用此处描述的解决方案使所有内容都可以在/ public中查看,并且尝试以下操作不起作用:

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

Thanks 谢谢

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 这样,您可以使用url /和/ public / image的内容访问/ public的内容/ url / images

Express 4: 快递4:

npm install serve-index

-- -

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM