expressjsHow can I create a REST API using Express.js?
Creating a REST API using Express.js is a straightforward process. Here is an example of a basic API that responds to a GET
request at the /hello
route:
const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server started');
});
Output example
Server started
The code above consists of four parts:
- Requiring the Express.js library:
const express = require('express');
- Creating an Express.js application:
const app = express();
- Defining a route and a callback function to respond to a
GET
request:app.get('/hello', (req, res) => { ... });
- Starting the server and listening on port 3000:
app.listen(3000, () => { ... });
For more information on creating a REST API using Express.js, please refer to the official Express.js documentation.
More of Expressjs
- How do I set the time zone in Express.js?
- How do I find Express.js tutorials on YouTube?
- How can I use Express.js and Vite together for software development?
- How can I set up auto reloading in Express.js?
- How do I download a zip file using Express.js?
- How can I use Express.js to yield results?
- How do I use Zod with Express.js?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How can I use Express.js and Winston together to create a logging system?
- How can I use Express.js to make an XHR request?
See more codes...