node express send error response

To send an error response in Node.js using the Express framework, you can follow these steps:

  1. Import the Express module:
const express = require('express');
  1. Create an instance of the Express application:
const app = express();
  1. Define a route that handles the error:
app.get('/error', (req, res) => {
  res.status(500).send('Internal Server Error');
});
  1. Start the server and listen for incoming requests:
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

In this example, we import the Express module and create an instance of the Express application. We then define a route using the app.get() method, specifying the path /error and a callback function that takes the request (req) and response (res) objects as parameters.

Inside the callback function, we use the res.status() method to set the HTTP status code to 500, indicating an internal server error. We then use the res.send() method to send the error message 'Internal Server Error' as the response body.

Finally, we start the server by calling the app.listen() method, specifying the port number (in this case, 3000) and a callback function that logs a message to the console once the server is running.

Note that this is a basic example, and you can customize the error response based on your specific requirements.