nodejs表示不拦截具有静态内容的文件夹的消息 [英] nodejs express not to intercept messages for a folder with static content

查看:326
本文介绍了nodejs表示不拦截具有静态内容的文件夹的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点,安装在主机上的快速系统。

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

所有请求都在 app.get(' / path'... 格式

然而在域中我有html文件夹,我要提供静态内容

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

并且不希望node / express拦截这些请求并让他们直接通过,甚至不通过nodej服务,否则相关链接问题。

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.

请建议一个解决方案。

推荐答案

您不能这样做。在默认情况下,节点不会提供任何内容 - 它不像其他Web服务器那样。

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'));

这表示任何以/ img开头的内容, code>应直接从 appDirectory +/ img目录中提供。 / 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.

这篇关于nodejs表示不拦截具有静态内容的文件夹的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆