通过 VHOST 连接的 Node.js 应用程序共享 404 页面
我对 Node.js 和 Web 编程总体来说还算陌生,所以如果我问一些奇怪的问题,请原谅:D
这就是我设置 Express Node.js 项目的方式。
我有一个顶级 app.js 只是将流量重定向到几个子域:
var app = module.exports = express.createServer(options);
app.use(express.vhost('blog.localhost', require('./apps/blog/blog.js')));
app.use(express.vhost('app1.localhost', require('./apps/app1/app1.js')));
app.use(express.vhost('login.localhost', require('./apps/login/login.js')));
在通过 require() 包含的每个子应用程序中,我只需返回一个新的快速服务器:
module.exports = express.createServer(options);
最优雅的设置方式是什么404页面?当我只使用单个应用程序时,我只是使用
app.use(function(req, res, next){
res.render('404', {
});
});
但如果我使用此方法,我将不得不为每个应用程序创建一个 404.jade,并且我不喜欢代码中无用的重复。知道如何在多个虚拟主机之间共享单个 404 逻辑吗?
I am sorta new to node.js and web programming in general so excuse if I ask strange questions :D
So here is the way I am setting up my express node.js project.
I have a top level app.js simply to redirect traffic to a few subdomains:
var app = module.exports = express.createServer(options);
app.use(express.vhost('blog.localhost', require('./apps/blog/blog.js')));
app.use(express.vhost('app1.localhost', require('./apps/app1/app1.js')));
app.use(express.vhost('login.localhost', require('./apps/login/login.js')));
In each of the sub-apps, that is included via require(), I simply return a new express server:
module.exports = express.createServer(options);
What is the most elegant way to set up a 404 page? When I was just using a single app, I simply used
app.use(function(req, res, next){
res.render('404', {
});
});
But if I use this method, I am going to have to create a 404.jade for every app and I dislike useless duplications in code. Any idea how to share a single 404 logic across multiple vhosts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有 3 台主机,需要 3 个路由文件。我的建议是在每个 404 路由中都要求来自外部文件。
这样,您就可以为每个主机提供 404 路由,但它只会驻留在一个位置。
示例:
404 路由文件 - 404.js
主机 A 的路由
You have 3 hosts that require 3 route files. My suggestion is to require in each this 404 route, from an external file.
This way you would have the 404 route for every host, but it will just reside in one place.
Example:
404 route file - 404.js
routes for host A