Skip to content

Backend

For node backend projects we mainly use Express to create our RESTful APIs. If you want to explore other options I would recommend you link into Fastify or Nestjs. As most of our projects are made with Express, only Express will be covered in this documentation for the moment.

Table of contents

Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. I can be used to create full stack applications but we mainly use it to create RESTful APIs.

Installation

To add it to your project run:

sh
yarn add express
npm
sh
npm install express --save

Hello world example (javascript)

Create an index.js file in the root of your project and paste

js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Then run :

sh
node index.js

File watching

You can add file watching and auto reload to your project by adding Nodemon to your project for development. This way you do not need to restart the project everytime you make a change.

Add to your project:

sh
yarn add nodemon -D
npm
sh
npm install nodemon -D

Then add a script to your package.json

json
{
...
 "scripts": {
    "dev":"nodemon index.js"
 }
...
}

You can now use it using the command:

sh
yarn dev
npm
sh
npm run dev

Hello world example (typescript)

For the typescript version of things there is a bit of a setup. We need a few packages to make things work.

First install the required development packages:

sh
yarn add typescript ts-node @types/express @types/node -D
npm
sh
npm install typescript ts-node @types/express @types/node -D

Then create a tsconfig.json file at the root of your project. You can use this configuration:

json
{
    "compilerOptions": {
        "sourceMap": true,
        "rootDir": "./src",
        "outDir": "dist",
        "strict": false,
        "lib": [
            "esnext"
        ],
        "esModuleInterop": true
    },
    "include": [
        "src/**/*"
    ]
}

Create an src/index.ts file in the root of your project and paste

ts
import type { Request, Response } from 'express'
import express from 'express'

const app = express()
const port = 3000

app.get('/', (_req: Request, res: Response) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Then run :

sh
node-ts index.js

Deployment

For a typescript projects to be deployed to production you must compile it down to javascript, to do this we simply add a build command to our package.json and refer to it in production

json
...
 "scripts": {
    "build":"tsc"
 }
...

File watching

You can add file watching and auto reload to your project by adding Nodemon to your project for development. This way you do not need to restart the project everytime you make a change.

Add to your project:

sh
yarn add nodemon -D
npm
sh
npm install nodemon -D

Create a nodemon.json configuration file for nodemon to work with typescript:

json
{
    "watch": [
        "src"
    ],
    "ext": "ts,json",
    "ignore": [
        "src/**/*.spec.ts"
    ],
    "exec": "ts-node ./src/index.ts"
}

Then add a script to your package.json

json
{
...
 "scripts": {
    "dev":"nodemon src/index.ts"
 }
...
}

You can now use it using the command:

sh
yarn dev
npm
sh
npm run dev

Released under the MIT License.