Day 3 - 60 Days of Learning 2025

Ashmin BhujelAshmin Bhujel
2 min read

Setting Up Project’s Basic Backend

On June 3, 2025, I initialized the repository for backend service of the project and configured the project by installing required dependencies and setting up project files and folder structure.

The GitHub link for the repository of beats-backend service.

Installing Basic Dependencies

Dependencies

  • express

  • mongoose

  • dotenv

  • cors

  • cookie-parser

Dev Dependencies

  • typescript

  • nodemon

  • ts-node

  • prettier

  • @types/cors

  • @types/cookie-parser

  • @types/express

Configuring tsconfig.json

Initialize the tsconfig.json file using pnpm tsc --init command and configure these properties.

{
    "compilerOptions": {
        "target": "esnext",
        "module": "commonjs",
        "rootDir": "./src",
        "outDir": "./build",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true
    }
}

Adding Scripts Inside package.json

"scripts": {
    "dev": "nodemon ./src/index.ts",
    "start": "node ./build/index.js",
    "build": "tsc -p ."
}

Configure .gitignore, .prettierrc and .prettierignore

.gitignore

node_modules
build
.env

.prettierrc

{
  "tabWidth": 2,
  "semi": true,
  "singleQuote": false,
  "bracketSpacing": true,
  "trailingComma": "es5"
}

.prettierignore

node_modules
build

Configure .env

Note: Replace with appropriate value of variables. This is an example of .env.example file.

# Node
NODE_ENV="<development|production>"

# Express
PORT=5000
CORS_ORIGIN="*"

Setup src/app.ts

Mostly setup related to Express is done inside this file.

import { config } from "dotenv";
import express, { Response } from "express";
import cors from "cors";
import cookieParser from "cookie-parser";

// Accessing environment variables
config();
const corsOrigin = process.env.CORS_ORIGIN;

// Express app
const app = express();

// Middlewares
app.use(
  cors({
    origin: corsOrigin,
    credentials: true,
  })
);
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));

// Root route
app.get("/", (_, res: Response) => {
  res.status(200).json({
    message: "Hello, World",
  });
});

// API v1 route
app.get("/api/v1", (_, res: Response) => {
  res.status(200).json({ message: "API v1" });
});

// Handle undefined routes
app.use((_, res: Response) => {
  res.status(404).json({ message: "This route is not defined" });
});

export { app };

Setup src/index.ts

This file is the entrypoint of the entire backend service.

import { config } from "dotenv";
import { app } from "./app";

// Accessing environment variables
config();
const port = process.env.PORT;

// Testing the setup
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Testing the Setup

Root Route

API v1 Route

Error/Undefined Route

The project setup was working properly and handling the request for /, /api/v1 and undefined routes with correct responses.

0
Subscribe to my newsletter

Read articles from Ashmin Bhujel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashmin Bhujel
Ashmin Bhujel

Learner | Software Developer | TypeScript | Node.js | React.js | MongoDB | Docker