gpt4 book ai didi

javascript - 在 Express.js 中处理路由的多个回调

转载 作者:行者123 更新时间:2023-12-03 06:26:35 26 4
gpt4 key购买 nike

我读到the Express.js documentation ,它讨论了使用如下语法处理路由回调:

app.get(path, callback [, callback ...])

但是,我似乎找不到一个很好的语法示例来处理所有 * 路由的多个回调。具体来说,路由处理程序需要:

1.) 使用 index.html 服务所有 View 请求,但是
2.) 通过返回适当的文件来为 index.html 中指定的资源提供回调请求。

需要什么特定语法来完成管理所有回调,以便 Express.js 服务器返回每个请求的资源文件,包括 JavaScript、CSS 等?

当我使用以下内容时:

app.get('*', function(req, res) {
console.log('inside * route!');
if(req.accepts('html')){
console.log('req.accepts html');
console.log('req.url is: '+ req.url);
}
if(req.accepts('text/html')){
console.log('req.accepts text/html');
console.log('req.url is: ' + req.url);
}
if(req.accepts('application/json')){
console.log('req.accepts application/json');
console.log('req.url is: ' + req.url);
}
if(req.accepts('json', 'text')){
console.log('req.accepts json, text');
console.log('req.url is: ' + req.url);
}
if(req.accepts('text/javascript')){
console.log('req.accepts html');
console.log('req.url is: ' + req.url);
}
if(req.accepts('text/css')){
console.log('req.accepts text/css');
console.log('req.url is: ' + req.url);
}
res.sendFile(path.resolve('dist/client/index.html')); // load the single view file (angular will handle the front-end)
});

结果是:

1.) index.html 响应每个文件类型的每个请求,包括 JavaScript、CSS 等。
2.) 条件语句不区分所请求文件的内容类型。

控制台打印出的记录是:

App listening on port 8080

inside * route!
req.accepts html
req.url is: /
req.accepts text/html
req.url is: /
req.accepts application/json
req.url is: /
req.accepts json, text
req.url is: /
req.accepts html
req.url is: /
req.accepts text/css
req.url is: /
GET / 304 30.181 ms - -

inside * route!
req.accepts html
req.url is: /boot.css
req.accepts text/html
req.url is: /boot.css
req.accepts application/json
req.url is: /boot.css
req.accepts json, text
req.url is: /boot.css
req.accepts html
req.url is: /boot.css
req.accepts text/css
req.url is: /boot.css

inside * route!
req.accepts html
req.url is: /vendor.js
req.accepts text/html
req.url is: /vendor.js
req.accepts application/json
req.url is: /vendor.js
req.accepts json, text
req.url is: /vendor.js
req.accepts html
req.url is: /vendor.js
req.accepts text/css
req.url is: /vendor.js

inside * route!
req.accepts html
req.url is: /boot.js
req.accepts text/html
req.url is: /boot.js
req.accepts application/json
req.url is: /boot.js
req.accepts json, text
req.url is: /boot.js
req.accepts html
req.url is: /boot.js
req.accepts text/css
req.url is: /boot.js
GET /boot.css 304 2.213 ms - -
GET /vendor.js 304 2.886 ms - -
GET /boot.js 304 2.638 ms - -

作为引用,Express.js 应用程序的根 index.html 是:

// set up ======================================================================
var express = require('express');
var app = express(); // create our app w/ express
var port = process.env.PORT || 8080; // set the port
var morgan = require('morgan'); // log requests to the console (express4)
var bodyParser = require('body-parser'); // pull information from HTML POST (express4)
var methodOverride = require('method-override'); // simulate DELETE and PUT (express4)

app.use(express.static(__dirname + '/dist/client')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(methodOverride());
app.use('/scripts', express.static(__dirname + '/node_modules/'));

// load the routes
require('./router')(app);

// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);

最佳答案

您想要做的事情可以使用 static files serving 来完成.

正如官方文档中所述,您可以使用以下代码来服务所有资源(.js.css 等):

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

关于javascript - 在 Express.js 中处理路由的多个回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38624644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com