Skip to content

Latest commit

 

History

History

README.md

Codever API (backend)

Back-end REST API for www.codever.dev based on Node.js with ExspressJS

Development

This section concerns about the local developemnt

Troubleshooting

Restart backend

It might happen to have a zombie running nodejs running on the port 3000 when trying to run npm run debug. Kill the process and try again

lsof -i tcp:3000
kill -9 xxx # where xxx is the process number from the command above
npm run debug # restart the backend server as usual

Testing

Unit tests

Run the test npm task

npm run test

Integration tests

To run the integration you have to prepare and start the local docker image and node for backend as specified in the main README file.

When ready run the following command

npm run test:integration

OpenAPI Docs

The API has an OpenAPI specification available at docs/openapi/openapi.yaml

Based on that a GUI is generated to test the API directly from browser:

Deployment

We currently use pm2 to start the project in production

Important – restart PM2 after deploying backend changes

PM2 keeps the old Node.js process running until explicitly restarted. After pulling new backend code (e.g. body-parser limit changes), always reload or restart the PM2 process:

pm2 reload pm2-process-cluster.json   # zero-downtime reload (preferred)
# or
pm2 restart pm2-process-cluster.json  # hard restart

Example: The Express body-parser is configured to accept up to 6 MB JSON payloads (app.js). If you deploy this change but forget to restart PM2, production still runs with the old default limit (100 KB) and larger requests (e.g. Jupyter notebook uploads) will fail with 413 Request Entity Too Large.

Nginx reverse proxy – large request bodies

If nginx sits in front of the API, its default client_max_body_size of 1 MB can also reject large requests before they reach Node.js. Add the following to the nginx server or location block that proxies to the API:

client_max_body_size 6m;

Then reload nginx:

sudo nginx -t && sudo systemctl reload nginx

Undo local changes if needed:

git fetch
git reset --hard origin/master

PM2

Cluster

## status
pm2 status

## start
pm2 start pm2-process-cluster.json

## restart - kills and restarts the process
pm2 restart pm2-process-cluster.json

## reload - in comparison with "restart", "reload" achieves 0-second-downtime reload
pm2 reload pm2-process-cluster.json

## stop
pm2 stop pm2-process-cluster.json

Single instance

# start
pm2 start pm2-process.json --env production --time

# restart
pm2 restart pm2-process.json --env production --time

# stop
pm2 stop pm2-process.json --env production

Commited is a pm2-process.exammple.json example file

Troubleshooting

Show pm2 logs

pm2 logs bookmarks.dev-api-node-10.15.0 --lines 1000

Show morgan logs

tail -f n100 ~/projects/bookmarks.dev-bk/backend/log/access.log

pm2 start errored

When pm2 start pm2 start pm2-process.json --env production has the status errored, it might help to delete the app id pm2 delete bookmarks.dev-api-node-10.15.0 and then try again