如何将两个 Node.js 应用服务器组合在一起。

发布于 2024-11-27 00:43:19 字数 1445 浏览 1 评论 0原文

我有两个应用程序。当前运行在两个不同的端口。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-12-04 00:43:19

如果您有指向此服务器的域或子域,您还可以使用 vhost 中间件:

app.use(express.vhost('s1.domain.com', require('s1').app));
app.use(express.vhost('s2.domain.com', require('s2').app));

app.listen(80);

完整示例: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:

app.use(express.vhost('s1.domain.com', require('s1').app));
app.use(express.vhost('s2.domain.com', require('s2').app));

app.listen(80);

Complete example: https://github.com/expressjs/express/blob/master/examples/vhost/index.js

゛时过境迁 2024-12-04 00:43:19

您可以使用 nginx 侦听端口 80 并将流量反向代理到其后面的 2 个不同的 Express 应用程序服务器。

location /s1/ {
    rewrite /s1(.*) $1 break;
    proxy_pass http://localhost:3000;
}

location /s2/ {
    rewrite /s2(.*) $1 break;
    proxy_pass http://localhost:3001;
}

您也可以按照您的要求在express中手动编码,但为什么要重新发明轮子呢?

You can use nginx listening on port 80 and reverse proxying traffic to the 2 different express app servers behind it.

location /s1/ {
    rewrite /s1(.*) $1 break;
    proxy_pass http://localhost:3000;
}

location /s2/ {
    rewrite /s2(.*) $1 break;
    proxy_pass http://localhost:3001;
}

You could also code this by hand in express as you are asking about, but why reinvent the wheel?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文