简体   繁体   English

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

[英]Handling multiple callbacks to a route in Express.js

I read the Express.js documentation , and it talks about handling callbacks to routes with a syntax like: 我阅读了Express.js文档 ,它讨论了如何使用以下语法处理对路由的回调:

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

However, I cannot seem to find a good example of syntax for handling multiple callbacks of the catch-all * route. 但是,我似乎找不到用于处理catch * all路由的多个回调的良好语法示例。 Specifically, The route handler needs to: 具体来说,路由处理程序需要:

1.) serve all requests for views with index.html , but then 1.)使用index.html满足所有视图请求,然后
2.) serve callback requests for assets specified in index.html by returning the appropriate file. 2.)通过返回适当的文件,为index.html指定的资产提供回调请求。

What specific syntax is required to accomplish manage all the callbacks so that the Express.js server returns every requested resource file, including JavaScript, css, etc? 完成所有回调的管理需要什么特定的语法,以便Express.js服务器返回每个请求的资源文件,包括JavaScript,css等?

When I use the following: 当我使用以下命令时:

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)
});

The result is: 结果是:

1.) index.html is served in response to every request for every file type, including JavaScript, css, etc. 1.)为响应每种文件类型(包括JavaScript,css等)的每个请求而提供index.html
2.) The conditional statements do not distinguish between content types of the requested file. 2.)条件语句不区分所请求文件的内容类型。

The console print out to document this is: 控制台打印出的文档如下:

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 - -

For reference, the root index.html for the Express.js app is: 作为参考,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);

What you want to do can be accomplished using static files serving . 您可以通过使用静态文件服务来实现。

As stated in the official docs, you can just use the following code, to serve all assets ( .js , .css , etc..): 如官方文档中所述,您可以仅使用以下代码来提供所有资产( .js.css等):

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

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

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