您可以使用自己的中间件路由到快速应用程序。所以一个应用程序用于大多数路线,一个 api 应用程序用于 api 路线。
示例:http ://runnable.com/UWx2MZS8trEHAACZ
var express = require('express');
var app = express();
var api = express();
app.configure(function () {
app.use(express.static(__dirname));
});
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/api*', api);
api.configure(function () {
api.use(function (req, res, next) {
process.stdout.write('checking');
setTimeout(process.stdout.write.bind(process.stdout, '.'), 100);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 200);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 300);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 400);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 500);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 600);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 700);
setTimeout(process.stdout.write.bind(process.stdout, '.'), 800);
setTimeout(process.stdout.write.bind(process.stdout, '.\n'), 900);
setTimeout(function () {
next();
}, 1000);
});
});
api.get('*', function (req, res) {
res.send('Hello API');
});
app.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);