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
GETrequest: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 use Yarn to add Express.js to my project?
- How do I find Express.js tutorials on YouTube?
- How do I use Express.js to parse YAML files?
- How can I set up X-Frame-Options in ExpressJS?
- How can I disable the X-Powered-By header in Express.js?
- How do I use Express.js and Yarn together in a software development project?
- How do I implement CSRF protection in an Express.js application?
- What is Express.js and how is it used for software development?
- How do I create a tutorial using Express.js?
- How can I use express-zip js to zip and download files?
See more codes...