During my journey through the Node.js universe I have missed a good template for what I wanted to do. I searched for a simple template for a REST service on which I can build on.
You can start with this by following the next couple of steps and are ready to go
- git clone https://firstname.lastname@example.org/m1hael/template-rest.git
- npm install
- npm run build
- npm start
This template also works out-of-the-box on IBM i.
If you have started the service you can test it by making the following HTTP request:
The template also provides a Swagger interface for the REST service at http://localhost:3000/swagger .
The directory structure of the project is pretty straight:
- config - configuration files
- logs - logging directory (if not configured)
- src - source code
- controller - REST endpoints
- docs - documentation
- models - data models
- startup - scripts which are executed during service startup like configuring logging
Application entry point is the
The template uses the config package for configuration. For most things a mapping to an environment variable is configured to keep it flexible.
The winston and morgan package has been used for logging. morgan has been used for creating an access log and winston for everything else. winston is configured to log in the logstash compatible format. The log entries also contains the application name as an additional field (see
middleware/logapp.ts) so that you can easily filter for the application when using something like the ELK stack.
The Startup class also accepts an object in the constructor. This can be used to pass in or get out values to/from the startup scripts. Each startup script is an implementation of the Runnable interface.
During development I like to have at least two terminals open: one for compiling the application with "npm run build:watch" and another for running the application with "npm run watch" (which uses the nodemon package). This ensures a short code-compile-run-test development cycle.