expressjsHow can I use Express.js to handle a request?
Express.js is a web application framework for Node.js that simplifies the process of handling requests. To use Express.js to handle a request, you need to create a server and define the routes.
Example code
// Include the Express.js library
const express = require('express');
// Create an Express.js server
const app = express();
// Define the route
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Start the server
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Output example
Server listening on port 3000
The code consists of the following parts:
const express = require('express');- This line includes the Express.js library.const app = express();- This line creates an Express.js server.app.get('/', (req, res) => {- This line defines the route for the request.res.send('Hello World!');- This line sends a response to the request.app.listen(3000, () => {- This line starts the server on port 3000.
Helpful links
More of Expressjs
- How do I use Express.js to parse YAML files?
- How can I set up X-Frame-Options in ExpressJS?
- How do I download a zip file using Express.js?
- How can I disable the X-Powered-By header in Express.js?
- How can I use Express.js and Vite together for software development?
- How do Express.js and Spring Boot compare in terms of features and performance?
- How do I use Express.js and Yarn together in a software development project?
- How can I make an XHR request using Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I configure Express.js to use Nginx as a reverse proxy?
See more codes...