-
Notifications
You must be signed in to change notification settings - Fork 0
How to serve with PM2
#How to serve with PM2
##Introduction Once you want your project to be served in production, you will want to ensure that it is continuously served. There are two issues with making this happen:
-
Starting a server through
npm startmight be fine (aside from non-production ready code), but the process would cease when the shell is closed. -
In many application setups, you can expect there to be a single file, such as
app.jsto run. In these cases,node app.jsmight do the trick. However, Webpack divvies up our dependencies. Because of this, we will actually need to run our server process on the whole/distfolder once it has been built.
With these two things in mind, let's get started.
##Build the application
Before we begin, you will need to build the application. On your server, run:
npm run build:prod
##Install PM2 on server
To install PM2, run the following on your server:
npm install pm2 -g
##Find where you've installed http-server
To begin serving your app through PM2, you will need to know where http-server is located. If you were to run npm run server:prod (which is located in our package.json) you would supply the /dist folder through whatever conditions you have supplied in the webpack.prod.config.js. A look at the package.json shows that http-server is called. In order to find out where http-server is located, run the following:
which http-server
##Finally, run PM2
Now that we know where http-server is located, we can make PM2 run it. If in the previous step your http-server was located at /usr/bin/http-server, you would run the following from your app's root folder:
pm2 start /usr/bin/http-server --name app -- -p 8080 -d false
In this command, you should replace /usr/bin/http-server with the location of your http-server. The --name app indicates the name you would like to give to the process. The -- allows for arguments to be passed to http-server. The -p 8080 sets port of the process. Running this all should serve your application through port 8080 of your server.
##Optional
At this point, you'll probably want a web server to catch the port and serve it. It is fairly simple to set up an nginx server to proxy_pass your traffic to the appropriate location and port.
enjoy — AngularClass
Learn AngularJS, Angular 2, and Modern Web Development from the best. Looking for corporate Angular training, want to host us, or Angular consulting? patrick@angularclass.com
