A custom NodeJS starter project with preconfigurations and templates to help you ship quality code early and often.
- Express Server
- Code Quality
- Development Workflow
- Docker Setup
- Jenkins Pipeline
- Automated Semantic Versioning
This project uses Express, a minimalist web framework for Node.js. It's lightweight and great for APIs.
Using nodemon, the express server is automatically restarted when the src
directory changes.
npm run server
The server defaults to port 3000
. You can change it by setting the PORT
environment variable.
In production, run the server using just node:
npm start
Jasmine is preconfigured and an example spec showing how to test Express apps is included. Just as important as testing but often forgotten is linitng. As author of Clean Code Robert Martin wrote:
Consistent indentation style was one of the most statistically significant indicators of low bug density.
Unit tests leverage the Jasmine testing framework. They are executed with the standard NPM test
command.
npm test
Unit specs:
- must use
.spec.js
as a suffix - are preferably saved in the
/specs
, but they will be picked up anywhere - are executed randomly
For your convenience context()
is also aliased to describe()
, which helps nested syntax:
describe ('login()', () => {
context ('valid credentials', () => {
it ('returns true', () => {
...
})
})
context ('invalid credentials', () => {
it ('returns false', () => {
...
})
})
})
This project is also linted for code consistency with the recommended rule set, plus some custom changes, most notably:
Semicolons are not allowed:
// OK
let foo = bar
// will error
let foo = 'bar';
Spaces are required before the function
parenthesis:
// OK
function (foo) {
return true
}
// Fails
function(foo) {
return true
}
Consistent spacing inside curly braces, for example:
// OK
import { app }
// Fails
import {app}
To see all adjustments, see the .eslintrc
file.
You can autofix most common linting offenses with this command:
npm run lint:fix
In development however, you can leverage auto reloading with:
npm run dev
which will simultaneously run and watch:
- server
- unit specs
- linter
Or you can run them separately:
npm run server
npm run lint:watch
npm run test:watch
Before I push code, I like to check if it will pass all continuous integration stages with a simple command that runs all the stages:
npm run preflight
If you wanted to, you could also configure a githook to ensure all your commits are green.
For your convenience, there is an included Dockerfile
and docker-compose.yml
, which help jumpstart your microservice architecture.
These presets save you from common gotchas:
- Dockerfile:
package.json
is copied first to avoid the heavynpm install
step on successive builds. docker-compose.yml
: note thatnode_modules
is mounted separately. This is required because native bindings can differ between the container and your development computer.- Included
.dockerignore
to avoid copying giantnode_modules
folder on build.
Note these files are configured for development only. If you plan to use them in production, you will make adjustments, e.g. use a different base image.
There is a sample Jenkinsfile
which a declarative pipeline including the following common stages:
- checkout
- install dependencies
- run linter
- run unit specs
This project uses standard-version for automatic semantic versioning and change logs.
-
Commit messages must follow the Conventional Commits standard.
-
When you are ready to release, just run:
npm run release
which will automatically:
- update your
CHANGELOG.md
- tag your git repository
Depending on your commit messages, standard-version will automatically determine if it's a major, minor or patch release.