如何将两个 Node.js 应用服务器组合在一起。
我有两个应用程序。当前运行在两个不同的端口。
script1.js:
var express = require('express'),
app = require('express').createServer(
express.cookieParser(),
// Parses x-www-form-urlencoded request bodies (and json)
express.bodyParser()
)
;
app.get('/s1/output', function(sReq, sRes){
// set cookie
sRes.send('<div>Out from 1!</div>');
});
app.listen(3000);
这是 script2.js
var express = require('express'),
app = require('express').createServer(
express.cookieParser(),
// Parses x-www-form-urlencoded request bodies (and json)
express.bodyParser()
)
;
app.get('/s2/output', function(sReq, sRes){
// set cookie
sRes.send('<div>Out from 2!</div>');
});
app.listen(3001);
好的..它在两个不同的端口上单独运行并且没有问题。
现在。故事是这样的,我只能使用端口 80 进行生产。系统管理员不想打开 3000 或其他端口。
而不是合并代码。 (事实上,我的真实代码很多。并且对 script1 和 script2 有不同的配置设置),我该怎么做才能使它们都在端口 80 上?但是调用 /s1/output 将转到 script1,而 /s2/output 将转到 script2?
我正在考虑编写另一个脚本。 script80.js 在端口 80 上运行。 它需要 script1 和 script2。
但是,问题是,我应该从脚本 1 和脚本 2 中导出什么?我应该:
define all get / post methods, and then,
module.exports.app =app?
在 script80.js 中,我应该做这样的事情吗:
app.get('/s1/*', function (res, req)) {
// and what do now? app1(res) ?
}
mmmm
I have two apps. which current run in two different ports.
script1.js:
var express = require('express'),
app = require('express').createServer(
express.cookieParser(),
// Parses x-www-form-urlencoded request bodies (and json)
express.bodyParser()
)
;
app.get('/s1/output', function(sReq, sRes){
// set cookie
sRes.send('<div>Out from 1!</div>');
});
app.listen(3000);
and here is script2.js
var express = require('express'),
app = require('express').createServer(
express.cookieParser(),
// Parses x-www-form-urlencoded request bodies (and json)
express.bodyParser()
)
;
app.get('/s2/output', function(sReq, sRes){
// set cookie
sRes.send('<div>Out from 2!</div>');
});
app.listen(3001);
ok.. it runs separately on two different ports and have no problem.
Now. the story is that, I can only use port 80 for production. System Admin doesn't want to open 3000 nor other ports.
Instead of merging code. (in fact, my real code is a lot. and have different config settings for script1 and script2), what can I do to make both of them on port 80? but calling /s1/output will go to script1, and /s2/output will go to script2?
I am thinking about having another scripts. script80.js that runs on port 80.
and it require both script1, and script2.
But, the question is, what should I export from script 1 and script2? should I:
define all get / post methods, and then,
module.exports.app =app?
and in script80.js, should I do soemthing like that:
app.get('/s1/*', function (res, req)) {
// and what do now? app1(res) ?
}
mmmm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有指向此服务器的域或子域,您还可以使用
vhost
中间件:完整示例:https://github.com/expressjs/express/blob/master/examples/vhost/index.js
If you have domains or subdomains pointing to this server, you can also use the
vhost
middleware:Complete example: https://github.com/expressjs/express/blob/master/examples/vhost/index.js
您可以使用 nginx 侦听端口 80 并将流量反向代理到其后面的 2 个不同的 Express 应用程序服务器。
您也可以按照您的要求在express中手动编码,但为什么要重新发明轮子呢?
You can use nginx listening on port 80 and reverse proxying traffic to the 2 different express app servers behind it.
You could also code this by hand in express as you are asking about, but why reinvent the wheel?