node.js并表达:可以从路由跳回中间件(静态)吗? [英] node.js and express: possible to skip from route back to middleware (static)?

查看:36
本文介绍了node.js并表达:可以从路由跳回中间件(静态)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Express Server编写我的node.js时,我希望首先在静态中间件之前运行路由中间件(希望在提供静态内容之前完全控制请求/响应).

When writing my node.js with express server I would like to first have the route middleware run before static middleware (want to have full control over the req/res before static content is being served).

现在,我还在末尾使用*匹配的路由以简单地返回404.显然,由于我没有用于静态内容的路由,因此需要为我的静态(公共)文件夹添加路由.这样做时,我想将控制权从路由内部传递到静态中间件,从而跳过我的404路由.那可能吗?我读到可以调用next("route"),但这给了我与调用next()相同的结果.

Now, I am also using a route matching on * at the end to simply return with a 404. Obviously, since I am not having routes for the static content, I need to add routes for my static (public) folder. When doing so, I would like to pass control from inside the route to the static middleware and thereby skip my 404 route. Is that possible? I read I could call next("route"), but this gives me the same result as calling next().

谢谢

推荐答案

您无需显式添加 * 路由.Express会为您执行404.

You don't need to add a * route explicitly. Express will do a 404 for you.

您需要做的就是告诉express在静态中间件之前运行自定义路由.您可以这样操作:

All you need to do is tell express to run the custom routes before the static middleware. You do this like so:

app.use(app.router);
app.use(express.static(__dirname + '/public');

这篇关于node.js并表达:可以从路由跳回中间件(静态)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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