简体   繁体   English

如果app-是子应用程序,则`app.use(express.static`似乎不起作用

[英]`app.use(express.static` seems to not working if app - is subapplication

I try to do something like this: 我尝试做这样的事情:

var main = express();
main.use(express.static(path.resolve('./asset')));
main.route('someroute', someHandle);
var app = express();
app.use(express.static(path.resolve('./asset')));
app.route('someroute', someHandle);
main.use('/app', app);

assets /asset/someasset.js served well, but /app/asset/someasset.js not returned (404), paths resolving to right folders. 资产/asset/someasset.js良好,但/app/asset/someasset.js没有返回(404),路径解析到正确的文件夹。

I tried app.use('/app', express.static(path.resolve('./asset'))); 我尝试了app.use('/app', express.static(path.resolve('./asset'))); - not work, but main.use('/app', express.static(path.resolve('./asset'))); -不起作用,但是main.use('/app', express.static(path.resolve('./asset'))); - works! -有效!

Is there some limitation to use express.static with mounted subapp? 有一些限制使用express.static与安装subapp?

UPD: UPD:

I try use mounted app as described in http://expressjs.com/ru/4x/api.html#express app.mountPath expecting that all features of express mounted as sub application should work in it, and as stumbled on static problem i would like to know is there are limitations in this use case? 我尝试使用http://expressjs.com/ru/4x/api.html#express app.mountPath中所述的挂载应用程序,期望Express挂载为子应用程序的所有功能都可以在其中使用,并且偶然发现了静态问题。想知道此用例是否有局限性? and what they could be? 他们可能是什么?

Your use case looks like a good candidate for Express Router, which is "an isolated instance of middleware and routes": 您的用例看起来像是Express Router的不错的选择,Express Router是“中间件和路由的隔离实例”:

http://expressjs.com/4x/api.html#router http://expressjs.com/4x/api.html#router

Specifically, try replacing 具体来说,请尝试更换

var app = express();

with

var app = express.Router();

Edit: Your use of path.resolve is all wrong. 编辑:您使用path.resolve都是错误的。

 path.resolve('./asset') 
in both cases resolves to the same folder. 在两种情况下都解析到相同的文件夹。 The mounting point of the middleware only affects the url and not the directory folder. 中间件的安装点仅影响url,而不影响目录文件夹。 Rewrite your code as suggested below and everything will work as advertised 按照下面的建议重写您的代码,所有内容都将如广告所示那样工作

My guess is express.static is still operating on the original path. 我的猜测是express.static仍在原始路径上运行。 So try this 所以试试这个

 var main = express(); main.use(express.static(path.resolve('./asset'))); main.route('someroute', someHandle); var app = express(); app.use(express.static(path.resolve('./app/asset'))); app.route('someroute', someHandle); main.use(app); 

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

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