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 can I use Express.js to generate a zip response?
- How do I use Yarn to add Express.js to my project?
- How do I find Express.js tutorials on YouTube?
- How can I use Node.js and Express together to create a web application?
- How can I use Express.js to yield results?
- How can I use an ExpressJS webhook to receive data from an external source?
- How can I use Express.js and Winston together to create a logging system?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How can I use Express.js with TypeScript?
See more codes...