好的,所以我有一个带有两个快速服务器的node.js应用程序。一个侦听端口80(HTTP),另一个侦听端口443(HTTPS)。
现在,这些路线没有相同的路线。恰恰相反,大多数事情是通过HTTPS提供的,有些是通过HTTP提供的。但是,有一些重叠。
所以,我想知道有没有办法将路由'绑定'到secureApp和App,因为我称之为我不需要两次写路由?
离。现在我有两套相同的路由(一个用于HTTP,一个用于HTTPS):
app.get('/tweet-stats.json', function(req, res){
res.set('Content-Type', 'application/json');
res.send(publicTweetStatus());
});
secureApp.get('/tweet-stats.json', function(req, res){
res.set('Content-Type', 'application/json');
res.send(publicTweetStatus());
});
我想做这样的事情:
model.get('/tweet-stats.json', function(req, res){
res.set('Content-Type', 'application/json');
res.send(publicTweetStatus());
});
app.handle.bind(model);
secureApp.handle.bind(model);
这可能吗?我会做更好的编码,我只管理所有端口上这个特定路由的一个实例。
答案 0 :(得分:7)
function secureOnly(req, res, next) {
if (!req.secure) {
res.send(426, 'sorry')
else {
next()
}
}
并在两个端口上收听
http.createServer(app).listen(80)
https.createServer(app).listen(443)