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:
yarn add expressnpm
npm install express --saveHello world example (javascript)
Create an index.js file in the root of your project and paste
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 :
node index.jsFile 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:
yarn add nodemon -Dnpm
npm install nodemon -DThen add a script to your package.json
{
...
"scripts": {
"dev":"nodemon index.js"
}
...
}You can now use it using the command:
yarn devnpm
npm run devHello 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:
yarn add typescript ts-node @types/express @types/node -Dnpm
npm install typescript ts-node @types/express @types/node -DThen create a tsconfig.json file at the root of your project. You can use this configuration:
{
"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
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 :
node-ts index.jsDeployment
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
...
"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:
yarn add nodemon -Dnpm
npm install nodemon -DCreate a nodemon.json configuration file for nodemon to work with typescript:
{
"watch": [
"src"
],
"ext": "ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "ts-node ./src/index.ts"
}Then add a script to your package.json
{
...
"scripts": {
"dev":"nodemon src/index.ts"
}
...
}You can now use it using the command:
yarn devnpm
npm run dev