简体   繁体   English

部署 Node.js + Express 到服务器

[英]Deploy Node.js + Express to server

Deploy Node.js + Express to server部署 Node.js + Express 到服务器

I am brand new to Node.js and Express development, and before I get too deep in a new project that I've sold my boss on using this newer tech on, I just want to test the technology on my server to make sure it all works.我是 Node.js 和 Express 开发的新手,在我向老板推销使用这种新技术的新项目之前,我只想在我的服务器上测试该技术以确保一切正常. We are using a dedicated server on Pair.com for hosting.我们在 Pair.com 上使用专用服务器进行托管。

Background, we've done all previous dev work in PHP/mySQL, and that's what our server is set up to run most efficiently, I guess.背景,我们已经在 PHP/mySQL 中完成了所有以前的开发工作,我猜这就是我们的服务器设置为最有效地运行的内容。

That being said, I've been able to SSH into the server, check that Node is installed (it is, and running version 12.18.1), CD into my domain, and install express.话虽如此,我已经能够将 SSH 安装到服务器中,检查 Node 是否已安装(已安装,并且运行版本 12.18.1),将 CD 放入我的域,然后安装 express。

I've got the following file I'm running using npm start, which runs this app.js file.我使用 npm start 运行以下文件,该文件运行此 app.js 文件。

var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', function (req, res) {
  res.send('Hey this is your website.');
});

app.get('/joke', function (req, res) {
  var joke =
    'What do you call a dog that does magic tricks? A labracadabrador.';
  res.send(joke);
});

app.listen(8080, function () {
  console.log('Server running on 8080!');
});

When I run this, I get “Server is running on 8080.”当我运行它时,我得到“服务器正在 8080 上运行”。 in my SSH terminal, However, if I visit that main domain or /joke.在我的 SSH 终端中,但是,如果我访问该主域或 /joke。 I get “page not found”.我得到“找不到页面”。

My theory is that Express is running on 8080, but my website is listening on port 80, and so while Express is technically running, my website isn't paying any attention to that app.js file.我的理论是 Express 在 8080 上运行,但我的网站正在侦听端口 80,因此当 Express 在技术上运行时,我的网站并没有关注该 app.js 文件。

If I try to run Express on port 80, I get an EACCESS error.如果我尝试在端口 80 上运行 Express,我会收到 EACCESS 错误。

My question is, how do I deploy and get my server to listen to the app.js file that is seemingly running on the server?我的问题是,如何部署并让我的服务器监听看似在服务器上运行的 app.js 文件?

Best solution is to use a reverse proxy to sit in front of your express server.最好的解决方案是使用反向代理坐在您的快递服务器前面。 The reverse proxy will listen on port 80 and forward requests to the express app listening on port 8080.反向代理将侦听端口 80 并将请求转发到侦听端口 8080 的 express 应用程序。

You could look into using nginx to accomplish this.您可以考虑使用nginx来完成此操作。 There are lots of tutorials out there on configuring it to work with express apps.有很多关于配置它以与快速应用程序一起使用的教程。

I once ran into a similar problem.我曾经遇到过类似的问题。 I solved it by using CORS middleware.我通过使用 CORS 中间件解决了这个问题。 I installed CORS on my Node server, and used it as a middleware like我在我的节点服务器上安装了 CORS,并将其用作中间件

app.use(cors({ origin: "http://localhost:3000" }));

Notice how i used the Client URL for origin.请注意我如何使用客户端 URL 作为来源。 You can replace the URL with your client URL.您可以将 URL 替换为您的客户端 URL。 I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM