Handling I the Express.js documentation lesen, und es spricht über Rückrufe an Routen mit einer Syntax wie Handhabung:mehrere Rückrufe auf einer Route in Express.js
app.get(path, callback [, callback ...])
Ich kann aber nicht ein gutes Beispiel für Syntax zu finden scheinen für Behandlung mehrerer Rückrufe der Catch-All *
Route. Insbesondere muss die Route Handler:
1.) dienen alle Anforderungen für Ansichten mit index.html
, aber dann
2.) dienen Rückrufe für bestimmte Vermögenswerte in index.html
durch die entsprechende Datei zurück.
Welche spezielle Syntax ist erforderlich, um alle Callbacks zu verwalten, damit der Express.js-Server jede angeforderte Ressourcendatei einschließlich JavaScript, CSS usw. zurückgibt?
Wenn ich folgendes:
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)
});
Das Ergebnis ist:
1.) index.html
als Antwort auf jede Anforderung für jeden Dateityp serviert wird, einschließlich JavaScript, CSS etc.
2.) Die bedingten Anweisungen unterscheiden nicht zwischen Inhaltstypen der angeforderten Datei.
Die Konsole Ausdruck dies zu dokumentieren ist:
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 - -
Als Referenz die Wurzel index.html
für die Express.js App ist:
// 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);
Können Sie ein Beispiel für eine statische Ressource schreiben Sie‘ Erfordert von index.html? – cmaster11