Skip to content

NodeJS template for building an express based application

License

Notifications You must be signed in to change notification settings

matteosacchetto/express-template

Repository files navigation

Express template

NodeJS template for building an express based server

Table of contents

Setup

Once the repository has been initialized from this template, or the template has been cloned, execute the following steps:

Set a LICENSE

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.

Update the package.json

In particular you need to modify/set the following attributes of the package.json:

  • name
  • version [OPTIONAL]
  • description
  • author
  • contributors [OPTIONAL]
  • license

Install the dependencies

You can install the dependencies by running the following command

npm install

This will take a while.

Start the application

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

Environment variables

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: .)

Modules

Express

Module used to create a server/RESTful API

Http and Https

Modules used to explicitly create a HTTP and HTTPS server

Morgan

Module used to log HTTP requests

Winston

Module used to define different loggers

Useful together with morgan

App root path

Module used to access the root path from any file, without the need to use '..' and '.'

Helmet

Module that helps to secure an express-based app by setting various HTTP headers. It’s not a silver bullet, but it can help!

Compression

Module used to compress responses. Useful especially in production since it helps to improve the performances

Http status codes

Helper module to get http status codes and messages

Express rate limit

Module to limit the amount of request that can be performed to the server

Dotenv

Module to load configuration from a .env file

HPP

Module to protect against HTTP Parameter Pollution attacks.

Express-mung

Module to create middlewares accessing the body data

Dev modules

Nodemon

Module used to auto-reload the app when changes are detected