» Node.js: Build a REST API with Express » 3. Deployment » 3.1 Standalone

Standalone Deployment

Run npm run build to build distribution folder:

npm run build

This is equivalent to tsc && tsc-alias. It will create a folder named dist in your project root.

Make sure you have the config.json config file along with your folder (as a sibling), and all yours databases are ready to serve.

Then, you can just run it as a standalone server:

node dist/main.js

Send some requests with curl, you will see something like this in the server logs:

::ffff:127.0.0.1 - - [02/Mar/2024:11:46:46 +0000] "GET /books/4/reviews?q=masterpiece HTTP/1.1" 200 1050 "-" "curl/8.1.2" - 14.394 ms
::ffff:127.0.0.1 - - [02/Mar/2024:11:46:47 +0000] "GET /books/4/reviews?q=masterpiece HTTP/1.1" 200 1050 "-" "curl/8.1.2" - 3.241 ms
::ffff:127.0.0.1 - - [02/Mar/2024:11:47:12 +0000] "GET /books HTTP/1.1" 200 1438 "-" "curl/8.1.2" - 9.047 ms

To make it work better, you may consider using PM2.

Use PM2 process manager

PM2 (Process Manager 2) is a production process manager for Node.js applications that allows you to keep your Node.js applications alive forever, reload them without downtime, and facilitate common system admin tasks. It's particularly useful in production environments where you need to ensure your Node.js applications are always running smoothly.

Install pm2:

npm install pm2 -g

Start your Node.js server with pm2:

pm2 start dist/main.js --name "books-api"

Check the status of your pm2 processes:

pm2 list

Result:

┌────┬──────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name         │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼──────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ books-api    │ default     │ 1.0.0   │ fork    │ 14849    │ 69s    │ 0    │ online    │ 0%       │ 61.7mb   │ literan  │ disabled │
└────┴──────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Monitor server logs:

pm2 logs books-api

Result:

[TAILING] Tailing last 15 lines for [books-api] process (change the value with --lines option)
/Users/literank/.pm2/logs/books-api-error.log last 15 lines:
/Users/literank/.pm2/logs/books-api-out.log last 15 lines:
0|books-ap | Connected to Redis
0|books-ap | Running on port 3000
0|books-ap | Connected to mongodb.
0|books-ap | Successfully initialized tables.
0|books-ap | ::ffff:127.0.0.1 - - [02/Mar/2024:11:52:07 +0000] "GET /books HTTP/1.1" 200 1438 "-" "curl/8.1.2" - 3.367 ms
0|books-ap | ::ffff:127.0.0.1 - - [02/Mar/2024:11:52:09 +0000] "GET /books HTTP/1.1" 200 1438 "-" "curl/8.1.2" - 1.271 ms
0|books-ap | ::ffff:127.0.0.1 - - [02/Mar/2024:11:52:09 +0000] "GET /books HTTP/1.1" 200 1438 "-" "curl/8.1.2" - 1.140 ms
0|books-ap | ::ffff:127.0.0.1 - - [02/Mar/2024:11:52:10 +0000] "GET /books HTTP/1.1" 200 1438 "-" "curl/8.1.2" - 1.205 ms

It works like a charm! ⭐️

PrevNext