Create Your First Express Application

Create a file named app.js (or index.js) in your project directory and add the following code:

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

// Middleware to handle JSON body
app.use(express.json());

// Route for the home page
app.get('/', (req, res) => {
  res.send('Hello World!');
});

// Route to handle POST requests
app.post('/data', (req, res) => {
  res.json({
receivedData: req.body
}); }); // Start the server app.listen(port, () => { console.log(Server is running on http://localhost:${port}); });

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *