如何运行节点。生产中的js +角4项目

时间:2022-06-06 23:08:44

I have an application using Angular 4 and Node.js that I run in development mode with angular-cli and nodemon.

我有一个使用角4和结点的应用。我使用angular-cli和nodemon在开发模式下运行的js。

Obviously Angular runs on :4200 and Node.js runs on :3000, and I made a proxy.config.json to redirect /api requests to :3000 as below:

显然,角在4200和节点上运行。js运行:3000,我做了一个proxy.config。json用于重定向/api请求:3000,如下:

{
  "/api/*":{
    "target":"http://localhost:3000",
    "secure":false,
    "logLevel":"debug"
  }
}

I run the project using two scripts that I defined in my package.json:

我使用我在packag中定义的两个脚本来运行这个项目。

"server": "nodemon server/server.js --watch server",
"dev": "ng serve --proxy-config proxy.config.json"

Now I want to go through the production mode and deploy my application on a VPS. What is the best way to run this application on a VPS?

现在我想要通过生产模式并在VPS上部署我的应用程序。在VPS上运行这个应用程序的最佳方式是什么?

1 个解决方案

#1


2  

1. Node.js deployment

With an Apache server, you can use Location and ReverseProxy to expose your Node.js API:

使用Apache服务器,您可以使用Location和ReverseProxy公开您的节点。js API:

/etc/apache2/site-availables/your-project.conf (on the <VirtualHost:.80> block, after DocumentRoot for example):

/etc/apache2/site-availables /您的项目。conf(在 <虚拟主机:。80> 块,以DocumentRoot为例):

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyVia Full
  <Proxy *>
    Require all granted
  </Proxy>
  <Location /api>
    ProxyPass http://127.0.0.1:3000
    ProxyPassReverse http://1127.0.0.1:3000
  </Location>

And then you can use pm2 to keep your Node.js application up in the background: How To Set Up a Node.js Application for Production on Ubuntu 16.04

然后可以使用pm2来保存节点。js应用在后台:如何设置节点。在Ubuntu 16.04上生产的js应用程序

2. Angular deployment

For the Angular project, simply build it for a production environment: ng build --prod (--aot)

对于角项目,只需为生产环境构建它:ng构建—prod(—aot)

Transfer the generated files on your server, and point your VirtualHost on it.

将生成的文件传输到服务器上,并将虚拟主机指向它。

#1


2  

1. Node.js deployment

With an Apache server, you can use Location and ReverseProxy to expose your Node.js API:

使用Apache服务器,您可以使用Location和ReverseProxy公开您的节点。js API:

/etc/apache2/site-availables/your-project.conf (on the <VirtualHost:.80> block, after DocumentRoot for example):

/etc/apache2/site-availables /您的项目。conf(在 <虚拟主机:。80> 块,以DocumentRoot为例):

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyVia Full
  <Proxy *>
    Require all granted
  </Proxy>
  <Location /api>
    ProxyPass http://127.0.0.1:3000
    ProxyPassReverse http://1127.0.0.1:3000
  </Location>

And then you can use pm2 to keep your Node.js application up in the background: How To Set Up a Node.js Application for Production on Ubuntu 16.04

然后可以使用pm2来保存节点。js应用在后台:如何设置节点。在Ubuntu 16.04上生产的js应用程序

2. Angular deployment

For the Angular project, simply build it for a production environment: ng build --prod (--aot)

对于角项目,只需为生产环境构建它:ng构建—prod(—aot)

Transfer the generated files on your server, and point your VirtualHost on it.

将生成的文件传输到服务器上,并将虚拟主机指向它。