RESTful API Server Boilerplate
Featuring Docker, Node, Express, MongoDB, Mongoose, & NGINX
License & Purpose
MIT License. This is something I've used in production before with success that I found useful for quickly bootstrapping RESTful APIs. You can fork and clone and take this apart without giving me any credit for anything. If you like it, you can star the repo
Feel free to make an issue or PR if you want to suggest ideas / fixes.
This configuration is a backend RESTful API boilerplate with the following pieces:
- Docker as the container service to isolate the environment.
- Express.js as the server framework / controller layer
- MongoDB as the database layer
- Mongoose as the "ODM" / model layer
- NGINX as a proxy / content-caching layer
How to Install & Run
- Fork/Clone the repo
docker-compose upto start three containers:
- the MongoDB database container
- the Node.js app container
- the NGINX proxy container
- Server is accessible at
http://localhost:5000if you have Docker for Windows or Mac. Use
http://localhostwithout specifying the port to hit the NGINX proxy. On Linux, you may need to hit the IP Address of the docker-machine rather than
localhost(port rules are the same.)
How to Run Tests
Currently, tests are run outside of the Docker container (unfortunately for now). The tests use an in-memory version of MongoDB. You should be able to run
npm install followed by
npm test to run everything (assuming you have the LTS version of Node installed on your machine).
- the boilerplate entity is called "Thing" and all the routes are based on the thing resource. When you want to start building a real API, you can probably just do a global find and replace for thing, but mind the case-sensitivity.
- most folders have an
index.jswhich simply exports the contents of all the files in those folders. This is to make importing things around the app slightly easier, since you can just
requirethe folder name and destructure the functions you are looking for. Check out this part of the Node.js docs for more info.
- this folder contains unit and integration tests both run using
npm testwhich in turn uses Jest
handlersare Express.js route handlers that have
helpersare raw JS "classes" and utility functions for use across the app
modelsare Mongoose schema definitions and associated models
routersare RESTful route declarations using express.Router module that utilize the functions in
schemasare JSONSchema validation schemas for creating or updating a Thing. Pro-tip: use JSONSchema.net to generate schemas based on examples for you.
app.jsis what builds and configures the express app
config.jsis the app-specific config that you will want to customize for your app
index.jsis the entrypoint that actually starts the Express server
- config contains NGINX proxy configuration, the production pm2 configuration (the process-runner of choice), and the Jest configuration to run MongoDB in memory