Create a Backend project with Node.js, Typescript and Docker

Create a Backend project with Node.js, Typescript and Docker

Looking for the frontend React project? Click here.

Step 1: Create a new Node.js project

Create a new Node.js project by:

mkdir saasbase-be
cd saasbase-be
npm init --yes
npm i typescript ts-node dotenv cors express
npm i -D ts-node-dev @types/node @types/express @types/cors

Add a new file called src/index.ts that will serve our Express server:

import express, { Application, Request, Response } from 'express';
import bodyParser from 'body-parser';

const app: Application = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/', (req: Request, res: Response) => {
  res.send('Healthy')
})

const PORT = process.env.PORT || 8000;

app.listen(PORT, () => {
  console.log(`Server is running on PORT ${PORT}`)
})

Create a tsconfig.json :

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  },
  "exclude": ["node_modules"]
}

Update package.json with:

"scripts": {
    "dev": "ts-node-dev index.ts",
    "build": "tsc",
    "start": "node dist"
  }

Run with:

npm run dev

This will run in Dev mode.

To compile and run a Production build:

npm run build
npm start

Step 2: Add env variables

Add dotenv to the very top of the src/index.ts file:

import * as dotenv from "dotenv";
dotenv.config();

...

console.log(process.env.SECRET_CODE);

Create a .env :

SECRET_CODE=1234

You can see the server print out the SECRET_CODE when you run the app.

Step 3: Add CORS

Add CORS in index.ts:

import cors from 'cors';

...

# if you want anyone to be able to connect
app.use(cors({ origin: true }))

# if you want only your frontend to connect
app.use(cors({ origin: "http://localhost:3000" }))

Step 4: Add gitignore

Make sure to add to .gitignore:

.env
node_modules

Step 5: Deploy to Heroku

Download Heroku CLI from here.

git init

git add .
git commit -am "first commit"

heroku apps:create saasbase-be
heroku git:remote -a saasbase-be
git push heroku master

heroku open

If you're deploying to Production, continue on.

Step 6: Add Dockerfile for production

FROM node:16 AS builder
WORKDIR /app
COPY package.json ./
RUN npm install 

COPY . ./
RUN npm run build

FROM nginx:1.19.0
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY --from=builder /app/dist .
ENTRYPOINT ["nginx", "-g", "daemon off;"]
docker build -t saasbase-be . --platform linux/amd64 # make sure the build is for correct platform

docker run -p 8000:8000 -d saasbase-be

Step 7: Upload image to Docker Hub

To create a repository:

  1. Sign in to Docker Hub.
  2. Click Create a Repository.
  3. Name it sssaini/saasbase-be
docker login

docker tag saasbase-be sssaini/saasbase-be:0.1
docker push sssaini/saasbase-be:0.1

FAQ

I can't install Heroku CLI on M1 Mac.

Follow instructions to set up Homebrew for M1 Macs here and try again.