expressjsHow can I get the IP address of a GET request using Express.js?
To get the IP address of a GET request using Express.js, you can use the req.ip
property. This property will return the IP address of the request.
For example:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
console.log(req.ip);
res.send('Hello World!');
});
app.listen(3000, () => console.log('Server started'));
Output example
Server started
The code above will print the IP address of the request to the console.
Code explanation
require('express')
- This imports the Express.js library into the application.app.get('/', (req, res) => {...})
- This defines a route handler for GET requests to the root path.console.log(req.ip)
- This prints the IP address of the request to the console.res.send('Hello World!')
- This sends 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...