node.js - 使用 Node.js 后端托管 50 多个网站(堆栈 : Forever, Express、Nginx 和 Let's Encrypt)

标签 node.js nginx

我计划使用 Node.js Express 服务器托管多个网站。在网站的根目录中为每个网站创建一个 server.js 文件是一个好习惯吗?

例如/var/www/example1.com/server.js & /var/www/example2.com/server.js。

示例 server.js:

const express = require('express')  
const app = express()  
const port = 3000 // 1 port for each website

app.get('/', (request, response) => {  
  response.send('Hello from Express!')
})

app.listen(port, (err) => {  
  if (err) {
    return console.log('something bad happened', err)
  }

  console.log(`server is listening on ${port}`)
})

带有启动每个服务器的规则的 Cronjob - 就像一个魅力

#!/bin/sh
export NODE_ENV=production
export PATH=/usr/local/bin:$PATH

for line in $(find /var/www/*/server.js); do
   forever start $line > /dev/null
done

最佳答案

是的,您需要为所有网站使用不同的 server.js,因为它们都可能处理不同的请求/具有不同的 API/不同的路由。

此外,如果您必须重新启动特定网站的服务器,并且仅使用一个 server.js,则您的所有网站都会在重新启动期间关闭,而不是仅需要更改的网站如果它们都有不同的 server.js

关于node.js - 使用 Node.js 后端托管 50 多个网站(堆栈 : Forever, Express、Nginx 和 Let's Encrypt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42214311/

相关文章:

node.js - 浏览器不会通过 gulp 和 livereload 重新加载

Docker Compose 与 NGINX 代理传递不起作用

azure - Azure 上的 nginx 配置

node.js - 我在哪里为node.js设置rejectUnauthorized = false

node.js - 使用 NodeJS 实例化一个 Admin SDK 目录服务对象

javascript - 如何使用 React 从 action.js 中的 API 获取响应值

Django:安全中间件使站点崩溃

javascript - 如何在 React 中反转数组

nginx - Docker + Nginx + ngx_http_image_filter_module

python - 在 Django View 中嵌入交互式 Bokeh