const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware to parse incoming JSON requests
app.use(bodyParser.json());
// Middleware to parse incoming URL-encoded requests
app.use(bodyParser.urlencoded({ extended: true }));
// Route handler for GET request at the root endpoint
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// Route handler for POST request at the '/api/data' endpoint
app.post('/api/data', (req, res) => {
const requestData = req.body;
// Process the incoming data (replace this with your logic)
// ...
res.json({ message: 'Data received successfully', data: requestData });
});
// Route handler for handling 404 errors (Not Found)
app.use((req, res) => {
res.status(404).send('Not Found');
});
// Route handler for handling other errors
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Internal Server Error');
});
// Start the server and listen on the specified port
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});