NodeJS template for building an express based server
Once the repository has been initialized from this template, or the template has been cloned, execute the following steps:
By default i configured the template to use a MIT license, but if you want to use a different license, change the LICENSE file and the license: "MIT"
attribute of the package.json file.
In particular you need to modify/set the following attributes of the package.json:
- name
- version [OPTIONAL]
- description
- author
- contributors [OPTIONAL]
- license
You can install the dependencies by running the following command
npm install
This will take a while.
You can start the application in two different ways
If you want hot reload, that is available through nodemon using the dev script
npm run dev
or using nodemon directly
nodemon .
Otherwise, if you want to start the server without hot reloading, you can either use the start script
npm start
or you can use node directly
node .
By default this will start a server on http://localhost:8000.
To modify these values you can simply create a .env file containing all required settings.
For example:
# Settings HTTP
USE_HTTP=true
PORT=4464
To see a richer example of the .env file take a look at the .env.example file in the root folder of the project
These are the environment variables that can be defined in the .env file
- USE_HTTP: to specify whether to start a HTTP server or not. (default: true)
- false: do not start the HTTP server
- true: do start the HTTP server
- PORT: the port on which the HTTP server will listen (default: 8000)
- USE_HTTPS: to specify whether to start a HTTPS server or not. (default: false)
- false: do not start the HTTPS server
- true: do start the HTTPS server
- PORT_HTTPS: the port on which the HTTPS server will listen (default: 44300)
- NODE_ENV: the environment {development, production} (default: development)
- SSL_KEY_PATH: to specify the path to the ssl key to use. (default 'ssl/key.pem') NOTE: it is only used if the NODE_ENV is set to production and USE_HTTPS is set to true
- SSL_CERT_PATH: to specify the path to the ssl certificate to use. (default 'ssl/cert.pem') NOTE: it is only used if the NODE_ENV is set to production and USE_HTTPS is set to true
- USE_STATIC: to specify whether to serve static files or not. (default: false)
- false: do not server static files
- true: do serve static files
- SERVE_SPA: to specify whether the static files are of a Single Page Application (SPA) or not. (default: false)
- false: they are not files of a SPA
- true: they are files of a spa
- STATIC_PATH: the path of the folder which contains static files. The path is relative with respect to the project folder (default: client/public)
- USE_STDOUT: specifies whether to log to files or to stdout (useful when in a docker container). (default: false)
- APP_ROOT_PATH: this is used to identify the root directory of the source code of the server. There is no need to specify it, since it is able to auto-decalare it, but it is available if for some reason you want to specify it. (default: .)
Module used to create a server/RESTful API
- Version: 4.21.1
- Website: https://expressjs.com/en
- Documentation: https://expressjs.com/en/4x/api.html
- Module name: express
Modules used to explicitly create a HTTP and HTTPS server
- Enviroment variables used:
- process.env.PORT: specify the HTTP port (default=8000)
- process.env.PORT_HTTPS: specify the HTTPS port (default=44300)
- Version: NodeJS version
- Documentation:
- Modules name:
- http
- https
Module used to log HTTP requests
- Enviroment variables used:
- process.env.NODE_ENV: specify the enviroment (default="development")
- Version: 1.10.0
- Website: https://www.npmjs.com/package/morgan
- Documentation: https://www.npmjs.com/package/morgan
- Module name: morgan
Module used to define different loggers
Useful together with morgan
- Enviroment variables used:
- process.env.NODE_ENV: specify the enviroment (default="development")
- Version: 3.15.0
- Website: https://www.npmjs.com/package/winston
- Documentation: https://www.npmjs.com/package/winston
- Module name: winston
Module used to access the root path from any file, without the need to use '..' and '.'
- Version: 3.1.0
- Website: https://www.npmjs.com/package/app-root-path
- Documentation: https://www.npmjs.com/package/app-root-path
- Module name: app-root-path
Module that helps to secure an express-based app by setting various HTTP headers. It’s not a silver bullet, but it can help!
- Version: 8.0.0
- Website: https://helmetjs.github.io/
- Documentation: https://helmetjs.github.io/docs/
- Module name: helmet
Module used to compress responses. Useful especially in production since it helps to improve the performances
- Version: 1.7.4
- Website: https://github.com/expressjs/compression
- Documentation: https://github.com/expressjs/compression
- Module name: compression
Helper module to get http status codes and messages
- Version: 2.3.0
- Website: https://www.npmjs.com/package/http-status-codes
- Documentation: https://www.npmjs.com/package/http-status-codes
- Module name: http-status-codes
Module to limit the amount of request that can be performed to the server
- Version: 7.4.1
- Website: https://www.npmjs.com/package/express-rate-limit
- Documentation: https://www.npmjs.com/package/express-rate-limit
- Module name: express-rate-limit
Module to load configuration from a .env file
- Version: 16.4.5
- Website: https://www.npmjs.com/package/dotenv
- Documentation: https://www.npmjs.com/package/dotenv
- Module name: dotenv
Module to protect against HTTP Parameter Pollution attacks.
- Version: 0.2.3
- Website: https://www.npmjs.com/package/hpp
- Documentation: https://www.npmjs.com/package/hpp
- Module name: hpp
Module to create middlewares accessing the body data
- Version: 0.5.1
- Website: https://www.npmjs.com/package/express-mung
- Documentation: https://www.npmjs.com/package/express-mung
- Module name: express-mung