Combine multiple node.js web applications

女生的网名这么多〃 提交于 2019-12-21 02:04:07

问题


I'm trying to figure out the best way to accomplish this; essentially I have about 6 websites I have to get online but at the moment they will have next to zero traffic so to save money they need to be deployed on the same server (ideally we will be using Elastic BeanStalk from AWS).

Is there a way to essentially write each web application like normal (so they can easily be taken and moved to a dedicated server in the future) but have one app.js entry point that appropriate loads the node application depending on the URL?

Obviously this isn't ideal but and I've thought of a couple of ways to do this but I want to be as non-hacky as possible so the sites can easily be moved later.


回答1:


Use connect vhost. http://www.senchalabs.org/connect/vhost.html

var express = require('express'),
    main = express();

main.use(express.vhost('*.site1.com', require('../site1')));
main.use(express.vhost('*.site2.com', require('../site2')));

main.listen(80);

And ../site1/index.js might look like this:

var express = require('express'),
    app = express();

app.get('/', function(req, res) { res.send('Home Page'); });

module.exports = app;


来源:https://stackoverflow.com/questions/18519846/combine-multiple-node-js-web-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!