Skip to content

not-soo-techie/ts-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧩 Lab: Setup Node.js + TypeScript (CommonJS) and Create POST /books Route

🎯 Objective

In this lab, you will learn how to:

  • Set up a Node.js project using TypeScript with CommonJS module syntax.
  • Configure TypeScript compiler (tsc) properly.
  • Run your TypeScript code directly using ts-node or nodemon.
  • Create one working API route POST /books to add a book to a JSON file.
  • Test it manually using Postman or curl.

This lab will teach you the foundation for writing backend APIs in TypeScript — without using import/export syntax.


⚙️ 1️⃣ Project Setup

Step 1 — Initialize a new Node.js project

mkdir ts-node
cd ts-node
npm init -y

⚡ 2️⃣ Install Dependencies

Install Express (for building the API):

npm install express
npm install --save-dev typescript ts-node @types/node @types/express nodemon

📘 3️⃣ Initialize TypeScript

Run this to create a TypeScript config file:

npx tsc --init

Now open the generated tsconfig.json and replace everything with this configuration 👇

{
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "rootDir": "./src",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",
    "skipLibCheck": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

📂 4️⃣ Folder Structure

Create these folders and files:

ts-node-commonjs-lab/
│
├── package.json
├── tsconfig.json
├── src/
│   ├── server.ts
│   ├── routes/
│   │   └── bookRoutes.ts
│   ├── controllers/
│   │   └── bookController.ts
│   └── data/
│       └── books.json
└── dist/   (will be generated after build)

📄 5️⃣ Write the Code

🧠 src/server.ts

🧠 src/routes/bookRoutes.ts

🧠 src/controllers/bookController.ts

📄 5️⃣.1 Helper function for fs

import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

// Recreate __dirname and __filename for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Path to JSON file
const dataFile = path.join(__dirname, '../data/books.json');

📄 6️⃣ Tweak changes

Add node and express type definitions to tsconfig.json

"types": ["node", "express"]

Add any in server.ts

(req: any, res: any);

⚡ 7️⃣ Add NPM Scripts

In your package.json, add these scripts:

"scripts": {
    "dev": "nodemon --watch src --exec \"node --loader ts-node/esm --experimental-specifier-resolution=node\" src/server.ts",
    "build": "tsc",
    "start": "node dist/server.js"
}

🚀 8️⃣ Run the App

Start your development server:

npm run dev

📡 9️⃣ Test the API

Now test your route POST /books.

Using curl

curl -X POST http://localhost:3000/books \
  -H "Content-Type: application/json" \
  -d '{"title":"Atomic Habits","author":"James Clear","year":2018}'

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published