expressjsHow do I create a GET route using Express.js?
To create a GET route using Express.js, you need to use the .get()
method. This method takes two arguments: the route path, and a callback function. The callback function takes two arguments, request
and response
. The request
argument is an object that contains information about the request, such as the query parameters, headers, and other data. The response
argument is an object that allows you to send a response to the client.
Below is an example of a GET route:
app.get('/', (request, response) => {
response.send('Hello, World!');
});
The code above will send the string Hello, World!
to the client when the /
route is requested.
The parts of the code above are:
app
: This is an instance of the Express application..get()
: This is the Express method for creating a GET route./
: This is the path of the route.(request, response)
: These are the arguments for the callback function.response.send()
: This is a method for sending a response to the client.
Helpful links
More of Expressjs
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I use express-zip js to zip and download files?
- How do I download a zip file using Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I disable the X-Powered-By header in Express.js?
- How can I use Express.js and Nest.js together to create a web application?
- How can I use Express.js to prevent XSS attacks?
- How do I use adm-zip with Express.js?
- How do I set the time zone in Express.js?
- How do I use Express.js to parse YAML files?
See more codes...