Project template for a web service using TypeScript and Node.js.
- Node.js Version 14.x
npm is the package manager for the Node JavaScript platform.
Using npm ci installs the exact versions of dependencies listed in the package-lock.json into the node_modules directory.
npm ci
The server is written in Typescript, but using ts-node the server can be run with realtime transpilation and not need a pre-compilation step.
For development, the server is started with npm start which runs the "start" script from the package.json.
npm start
This starts a HTTP server listening to port 3000 and serves a json "Hello World" response.
View the Hello World in a web browser.
{
"message": "Hello World"
}The application is running in a "watch" mode, watching for file changes and will restart when any source file changes.
Visual Studio Code is setup to debug the service using launch.json.
The default command for running tests in node projects is npm test which runs the test script from package.json.
npm test
Jest is a JavaScript Testing Framework with a focus on simplicity. Using ts-jest, jest is configured in the "jest" section of package.json. Jest is configured to enforce 100% code coverage. This is a best practice to ensure either code is tested or explicitly marked as not tested.
Install the jest plugin for Visual Studio Code which runs the tests in the editor, giving real-time feedback. The plugin also adds a debug link for debugging tests in Visual Studio Code.
Code linting checks are run using the "lint" script defined in package.json.
npm run lint
ESLint statically analyzes your code to quickly find problems. ESLint is configured in the eslint section of the package.json. Linting rules are setup make cleaner code, reduce errors, and create a maintainable code base.
Install the ESLint plugin for Visual Studio Code.
Code formatting checks are run using the "check" script defined in package.json.
npm run check
The code formatting is enforced using Prettier - an opinionated code formatter.
By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles. It is generally accepted that having a common style guide is valuable for a project and team but getting there is a very painful and unrewarding process. People get very emotional around particular ways of writing code and nobody likes spending time writing and receiving nits.
Prettier is configured in the prettier section of the package.json.
Install the Prettier plugin for Visual Studio Code.
Security vulnerabilities are sometimes found in NPM packages. npm audit scans your project for vulnerabilities and can automatically install any compatible updates to vulnerable dependencies.
npm audit
Dependency upgrades can be run using the "update" script defined in package.json.
npm run update
This will update dependencies, run audits, and run tests.
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
This project contains a Dockerfile that builds off the public DockerHub official node docker image to create a docker image for this application.
The docker image is built using the "docker:build" script defined in package.json.
npm run docker:build
The docker container can be run using the "docker:run" script defined in package.json.
npm run docker:run