Deployment
If you wish to deploy the code online to get your Public IP address then follow the steps.
module.exports = app;
add the following line to your code so that your code looks like this
const app = require("express")().get("/*", (req, res) => {
res.send({
IP:
req.headers["x-forwarded-for"]?.split(",").shift() ||
req.socket?.remoteAddress,
});
});
app.listen(3000, () => console.log("Listening on port 3000"));
module.exports = app;
(install vercel CLI and login to vercel with npm i -g vercel )
Make vercel.json in the root directory and paste the following code
{
"version": 2,
"routes": [
{
"src": "/",
"dest": "/api"
}
],
"builds": [
{
"src": "api/index.js",
"use": "@vercel/node"
}
]
}
Next, Deploy the code to vercel with
vercel deploy
and Congrats you have an API that gives you your IP address
https://get-ip-eight.vercel.app/
https://get-ip-eight.vercel.app/api
3
