[英]node js express route all paths
I am using express. 我正在使用快递。 I like to create a route that navigates all requests of type "get" with url prefix '/app/static/*pagePath' to "/assets/app/static/pagePath". 我想创建一条路由,将所有带有URL前缀“ / app / static / * pagePath”的“ get”类型的请求导航到“ / assets / app / static / pagePath”。 I am trying to do the following, but is doesn't work. 我正在尝试执行以下操作,但不起作用。
app.get('/app/static/*path', function (req, res) {
res.sendfile('assets/app/static/' + req.params.path);
});
Any idea? 任何想法?
Just use a middleware with a prefix and some short-circuit logic: 只需使用带有前缀和一些短路逻辑的中间件即可:
app.use('/app/static', function (req, res, next) {
if (req.method !== 'get') {
next();
return;
}
res.sendFile(__dirname + '/assets' + req.path);
});
(this is untested so might not be 100% ready to go, but you get the idea) (这未经测试,因此可能尚未100%准备就绪,但您知道了)
Actually, looking again at your question, are you sure this can't be handled by the express.static
middleware just given the proper root directory? 实际上,再次查看您的问题,您确定仅赋予正确的根目录就不能通过express.static
中间件来解决?
app.use('/app/static', express.static(__dirname + '/assets'));
If you want to include subdirectories, you can do it using a regex; 如果要包括子目录,则可以使用正则表达式来完成。 this regex will match any directory/file structure under /app/static/
此正则表达式将匹配/app/static/
下的任何目录/文件结构
app.get(/^\/app\/static\/(.*)/, function (req, res) {
console.log('assets/app/static/' + req.params[0]);
res.sendfile('assets/app/static/' + req.params[0]);
});
As to your question about multiple static directories, yes, you can. 关于多个静态目录的问题,可以。 Just app.use
both; 只需应用app.use
。
app.use("/static", express.static(__dirname + "/assets"));
app.use("/static", express.static(__dirname + "/alternate_assets"));
The file will be served from the first directory where it's found (searching /assets
first, then /alternate_assets
), but to make things less confusing, you may want to avoid having the same file name in both directories. 该文件将从找到该文件的第一个目录提供(先搜索/assets
,然后搜索/assets
/alternate_assets
),但是为了减少混乱,您可能要避免在两个目录中使用相同的文件名。
None of the other answers had worked for me in the past because they were missing two small details: 过去没有其他答案对我有用,因为它们缺少两个小细节:
app.use
calls to define static directories should be async (eg wrapped in a function), or they will try to execute before the websocket has finished establishing its connection (in my opinion, this is an issue with Connect -- the framework which Express is built on top of; I should be able to just start writing my code at the top of the file). 定义静态目录的app.use
调用应该是异步的(例如,包装在函数中),否则它们将在websocket完成建立连接之前尝试执行(我认为,这是Connect的问题-Express的框架是建立在上面;我应该能够开始在文件顶部开始编写代码)。
These definitions need to happen before you send the HTML file that would have references to the URL, within that (async) function body. 这些定义需要在您发送(异步)函数体内发送对URL引用的HTML文件之前进行 。
Like this: 像这样:
app.get('/', function (req, res) {
app.use('/static', express.static(__dirname + '/static'));
res.sendfile(__dirname + '/index.html');
});
And then you can include them in directly in your HTML (or SVG, etc.) like this: 然后,您可以将它们直接包含在HTML(或SVG等)中,如下所示:
• <link rel="stylesheet" href="static/index.css">
• <link rel="stylesheet" href="static/index.css">
• <image x="0" y="0" xlink:href="static/picture.svg" />
• <image x="0" y="0" xlink:href="static/picture.svg" />
• etc. •等
GET /app/static/foo/bar/buz ---> req.path === /app/static/foo/bar/buz so: GET / app / static / foo / bar / buz ---> req.path === / app / static / foo / bar / buz如下:
app.get('/app/static/:path*', function (req, res) {
res.sendfile('assets' + req.path);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.