Welcome to the Node.js and Express.js repository with code examples !
Abhishek Rajpoot
- Installation Guide
- Basic Express Server
- Middleware in Express
- Routing in Express
- Handling Requests and Responses
- Working with Static Files
- Error Handling
To set up the repository on your local machine, follow these steps:
-
Clone the repository:
git clone https://github.com/exclusiveabhi/node-and-express.git
-
Navigate to the project directory:
cd node-and-express
-
Install dependencies:
npm install
-
Run the application:
node index.js
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}`);
});
server.use((req, res, next) => {
console.log('Middleware executed');
next();
});
server.get('/hello', (req, res) => {
res.send('Hello from the /hello route!');
});
server.post('/data', (req, res) => {
res.send('Data received via POST');
});
server.get('/user/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID is ${userId}`);
});
server.use(express.static('public'));
server.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something went wrong!');
});
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!