在Express.js中处理对路由的多个回调
Handling multiple callbacks to a route in Express.js
我阅读了Express.js文档,其中谈到了如何使用以下语法处理对路由的回调:
app.get(path, callback [, callback ...])
然而,我似乎找不到一个很好的语法示例来处理catch-all *
路由的多个回调。具体来说,路由处理程序需要:
1.)使用index.html
服务所有视图请求,但随后
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);
您想要做的事情可以使用静态文件服务来完成。
正如官方文档中所述,您只需使用以下代码即可为所有资产(.js
、.css
等)提供服务:
app.use('/static', express.static(__dirname + '/public'));
相关文章:
- AngularJS:我可以跳过函数参数回调吗
- 要求未定义JS回调参数
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 自引用回调
- 测试Angular Service解决错误回调中的promise
- 如何将一个JavaScript函数回调为多个函数
- JavaScript回调函数
- Ember我如何知道路由模板何时完成加载并触发回调
- Angular 2:当我离开路由时,如何应用回调
- AngularJs在app.config中的回调中配置路由
- Nodejs expressJS 路由,参数回调未触发
- 如何在路由中呈现回调JSON响应
- 在切换路由时清理请求和回调
- 在 expressjs 中的回调之间传递路由参数的最佳方法
- ExpressJs在为每个路由发送响应后执行回调
- Express:如何计算路由回调
- 将路由段作为回调参数传递
- 在Express.js中处理对路由的多个回调
- 对快速路由使用回调函数时的未定义参数
- 在初始化回调之前,不要在主干中呈现路由