nodejs express not to intercept messages for a folder with static content

允我心安 提交于 2019-12-31 07:00:29

问题


I've a node, express system installed working on a host.

All requests are going through in the app.get('/path'... format

however in the domain I've html folder with static content that I want to serve

http://domain.com/html/attendee

http://domain.com/html/sponsors

and don't want node/express to intercept these requests and let them go through directly, not even serve through nodejs, otherwise relative linking problem.

Please suggest a solution.


回答1:


You can't do it that way. node doesn't serve ANY content by default - it is not like some other web servers in that regard.

Instead, you specifically configure express to serve content from certain paths directly from the file system by inserting the right middleware commands early in the middleware stack.

For example, in one of my node apps, I use this middleware:

// static routes that get no further processing
app.use('/img', express.static(__dirname + '/img'));
app.use('/lib', express.static(__dirname + '/lib'));

This tells express that any content that starts with "/img" should be served directly from the appDirectory + "/img" directory. Same for elements in "/lib". One nice thing about this is that the paths you expose to the outside world do not have to be the same as the paths you use on your server and, in fact, by changing a few characters in your code, you can easily map to different directory.



来源:https://stackoverflow.com/questions/26771139/nodejs-express-not-to-intercept-messages-for-a-folder-with-static-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!