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}`);
});
Leave a Reply