Node.js API Development Guide

Node.js API Development Guide
Muhammad Zubair
Full Stack Development
2/6/2026

Node.js API Development Guide

Node.js Topics

REST APIs with Express

Express framework APIs create karna fast aur easy banata hai, backend aur frontend ke liye perfect hai.

import express from "express"
const app = express()

app.get("/users", (req, res) => {
  res.json([{ name: "Ali" }, { name: "Zubair" }])
})

app.listen(3000, () => console.log("Server running"))



Middleware for Request Handling


Middleware requests process karte hain, logging aur authentication easy banata hai, backend modular hota hai.

app.use((req, res, next) => {
  console.log(`${req.method} ${req.url}`)
  next()
})




Asynchronous Operations


Async/Await promises handle karna readable aur efficient banata hai, performance improve hoti hai.

import fs from "fs/promises"

async function readFile() {
  const data = await fs.readFile("data.txt", "utf8")
  console.log(data)
}

readFile()



Environment Variables for Security

Sensitive data jaise API keys aur DB credentials secure rakhne ke liye environment variables use hote hain.

import dotenv from "dotenv"
dotenv.config()

console.log(process.env.DB_PASSWORD)



File Upload Handling

Node.js backend file uploads handle kar sakta hai, jaise images aur documents, middleware ke saath seamless handling.

import multer from "multer"
const upload = multer({ dest: "uploads/" })

app.post("/upload", upload.single("file"), (req, res) => {
  res.send("File uploaded successfully")
})



Real-Time Communication

Socket.io real-time features provide karta hai, jaise chat aur notifications, interactive apps ke liye perfect hai.

import { Server } from "socket.io"
const io = new Server(3000)

io.on("connection", (socket) => {
  console.log("User connected")
})



Error Handling

Proper error handling app crash se bachata hai aur debugging easy banata hai.

app.use((err, req, res, next) => {
  console.error(err.stack)
  res.status(500).send("Something went wrong")
})



Authentication with JWT

JWT authentication secure login aur token-based authorization provide karta hai, modern apps ke liye essential hai.

import jwt from "jsonwebtoken"
const token = jwt.sign({ userId: 1 }, "secretkey")
console.log(token)



Scheduling Tasks

Node-Cron background tasks aur automated jobs run karne ke liye use hota hai, jaise emails ya cleanup.

import cron from "node-cron"

cron.schedule("* * * * *", () => {
  console.log("Task running")
})



Environment-Based Configurations

Config files aur environment variables se app different environments me smoothly run hota hai.

const config = process.env.NODE_ENV === "production" ? prodConfig : devConfig
console.log(config)