Express.js

Installing Express

To use Express in your project:

npm install express

Creating a Basic Express Server

// Import the express module
const express = require('express');

// Call the express function to create an app/server instance
const app = express();

// Define the port number on which the server will listen
const PORT = 3000;

/**
 * GET request handler for the root URL ('/')
 * When the user accesses <http://localhost:3000/> with a GET request,
 * this callback will run and send a response.
 */
app.get('/', (req, res) => {
  res.send("Hello World");
});

/**
 * POST request handler for the root URL ('/')
 * When a POST request is made to <http://localhost:3000/>,
 * this callback will handle the request.
 */
app.post('/', (req, res) => {
  res.send("Hello Earth");
});

/**
 * PUT request handler for updating data
 * When a PUT request is made to <http://localhost:3000/>,
 * this callback will respond accordingly.
 */
app.put('/', (req, res) => {
  res.send("Updated the resource on Earth 🌍");
});

/**
 * DELETE request handler for deleting data
 * When a DELETE request is made to <http://localhost:3000/>,
 * this callback will handle it.
 */
app.delete('/', (req, res) => {
  res.send("Deleted the resource from Earth 🌍");
});

/**
 * Start the server and make it listen on the defined port
 * This function will run when the server is successfully started
 */
app.listen(PORT, () => {
  console.log(`✅ Server is running at <http://localhost>:${PORT}`);
});

Nodemon:

Nodemon is a handy development tool for Node.js applications that automatically restarts the server whenever it detects changes in the source code. Instead of manually stopping and restarting the server after every update, Nodemon watches for file changes in the project directory and restarts the application on its own. This significantly improves development speed and convenience, especially when working on APIs or backend logic.

Nodemon is installed as a development dependency or globally using npm install -g nodemon or npm install --save-dev nodemon, and can be run in place of the node command. For example, nodemon app.js will start the application with auto-reload enabled.


tsconfig.json (TypeScript Configuration)

The tsconfig.json file is the core configuration file for TypeScript projects. It defines how TypeScript should compile your code, what files to include or exclude, and how to handle module resolution.

Here’s a breakdown of a typical tsconfig.json used in a Node.js server project:

{
  "compilerOptions": {
    "target": "ES6",                       // Output JavaScript version
    "module": "commonjs",                  // Module system to use (Node.js uses CommonJS)
    "rootDir": "./src",                    // Source code directory
    "outDir": "./dist",                    // Output directory for compiled JavaScript files
    "strict": true,                        // Enable strict type checking
    "esModuleInterop": true,               // Allow default imports from modules with no default export
    "skipLibCheck": true,                  // Skip type checking of declaration files (*.d.ts)
    "forceConsistentCasingInFileNames": true // Enforce consistent file casing
  },
  "include": ["src"],                      // Folders/files to include in the compilation
  "exclude": ["node_modules", "dist"]      // Folders/files to exclude
}

Important Keys