Open Source on IBM i

Node.js Template Project

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

This template also works out-of-the-box on IBM i.

I like the RESTED client for Firefox to test my services and also store the request for later reuse. The add on is also available for Chrome.

If you have started the service you can test it by making the following HTTP request:

GET http://localhost:3000/api/date?format=dd.MM.yyyy

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
    • middleware
    • models - data models
    • startup - scripts which are executed during service startup like configuring logging
    • services

Application entry point is the src/server.ts file.

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.

Happy coding!

Mihael

Tags : node.js