Skip to content

Node.js and Express.js . This repository serves as a comprehensive guide for anyone looking to understand and master the basics of Node.js and Express.js, two of the most popular technologies for building robust and scalable web applications.

Notifications You must be signed in to change notification settings

exclusiveabhi/node-and-express

Repository files navigation

Node.js and Express.js

Welcome to the Node.js and Express.js repository with code examples !

Repository URL

GitHub Repository

Author

Abhishek Rajpoot

Table of Contents

Installation Guide

To set up the repository on your local machine, follow these steps:

  1. Clone the repository:

    git clone https://github.com/exclusiveabhi/node-and-express.git
  2. Navigate to the project directory:

    cd node-and-express
  3. Install dependencies:

    npm install
  4. Run the application:

    node index.js

Basic Express Server

const express = require('express');
const server = express();
const port = 3000;

server.get('/', (req, res) => {
    res.send('Hello, World!');
});

server.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});

Middleware in Express

server.use((req, res, next) => {
    console.log('Middleware executed');
    next();
});

Routing in Express

server.get('/hello', (req, res) => {
    res.send('Hello from the /hello route!');
});
server.post('/data', (req, res) => {
    res.send('Data received via POST');
});

Handling Requests and Response

server.get('/user/:id', (req, res) => {
    const userId = req.params.id;
    res.send(`User ID is ${userId}`);
});

Working with Static Files

server.use(express.static('public'));

Error Handling

server.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something went wrong!');
});

Conclusion

This repository covers the important topic of Node.js and Express.js. Feel free to explore the examples and modify them to suit your needs. For more information, refer to the official Node.js documentation and Express.js documentation.

Happy coding!

About

Node.js and Express.js . This repository serves as a comprehensive guide for anyone looking to understand and master the basics of Node.js and Express.js, two of the most popular technologies for building robust and scalable web applications.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published