I need to create a REST API endpoint for my web application. Can you provide an example of how to do that using Node.js and Express?

Sample response
Sure, here's an example of how to create a simple REST API endpoint using Node.js and Express: First, make sure you have Node.js and the Express framework installed on your machine. You can install them by running the following commands: ``` npm install node npm install express ``` Then, create a new file called `server.js` and add the following code: ```javascript const express = require('express'); const app = express(); // Define a GET endpoint app.get('/api/hello', (req, res) => { res.send('Hello World!'); }); // Start the server const port = process.env.PORT || 3000; app.listen(port, () => { console.log(`Server is listening on port ${port}`); }); ``` In this example, we're creating a simple REST API endpoint that responds to a GET request to `/api/hello` with a "Hello World!" message. To start the server, run the following command: ``` node server.js ``` You should see the message "Server is listening on port 3000" in the console. You can then access the API endpoint by navigating to `http://localhost:3000/api/hello` in your web browser or using a tool like cURL or Postman. That's it! You've created a simple REST API endpoint using Node.js and Express.
Published: a year ago