Getting complete ip addrress in express, node.js - node.js

I was trying to get complete IP address of the client using express and node.js but what I am getting is ::1. I tried reading this How to get IP address in node.js express not could not find the solution
Here is my code.
const express = require('express')
const app = express()
const middleware = (req, _, next) => {
console.log(req.ip)
// Here I am getting ::1
next()
}
app.get("/*",middleware, (req, res) => {
res.send("Hello")
})
app.listen(3000,() => console.log("Server started"))
The above code is in the index.js file
Here is my package.json file
{
"name": "one",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Can anyone please tell me how how can I get complete ip address?

::1is the IPv6 equivalent of 127.0.0.1 - the loopback address. That is to be expected if you're connecting to your server from the same computer and using localhost as the hostname.
If you connect to your server from a different computer, you should see an actual client IP address.

Related

Receive data on front-end React app from webhook

I am building a headless CMS with Strapi. I am testing the webhooks section and want to show the received data from the webhook on my React front-end.
I created a new folder webhooks on my local machine and ran npm init -y.
It created a package.json file with this content in it:
{
"name": "webhooks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}
I added a index.js file to the root folder with this content and installed express and body-parser:
const express = require("express")
const bodyParser = require("body-parser")
const app = express()
const PORT = 3001
app.use(bodyParser.json())
app.post("/webhook", (req, res) => {
console.log(req.body)
res.status(200).end()
})
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`))
After that I added this line in my package.json:
"start": "node index.js"
So it will start up with npm start instead of node index.js.
I added this URL to my strapi webhooks: http://localhost:3001/webhook and tested the trigger from the Strapi admin. It works fine.
After this I ran npx create-react-app client to create my react front-end app.
My next question is now how can I receive the contents from the webhook in my react front-end app?

Node.js in Heroku

I am hosting my very simple nodejs server in Heroku. But, when I try it, it returns this error:
Application error
An error occurred in the application and your page could not be served. If you are the application
owner, check your logs for details. You can do this from the Heroku CLI with the command`
Here's the server.js:
const express = require("express");
const cors = require("cors");
const PORT = process.env.PORT || 80;
const server = express();
server.use(cors());
server.get("/", (req, res) => {
const INDEX = "/index.html";
res.sendFile(INDEX, { root: __dirname });
});
server.get("/test", (req, res) => {
res.send("test Page");
});
server.listen(PORT, () => console.log(`listening on port ${PORT}`));
package.json:
{
"name": "express-heroku",
"version": "1.0.0",
"description": "",
"main": "server.js",
"engines": {
"node": "15.11.x"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.11.19"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
Don't know what the reason is, but, when I try this in the localhost it works perfectly!
The full error on Heroku CLI:
Any help is greatly appreciated !
Create a ProcFile:
Inside the ProcFile add web: node index.js
Doing this, you are telling heroku to run your server, with node.

Express-subdomain not seeing route

I am using the example setup from the https://www.npmjs.com/package/express-subdomain package. However, when I try api.localhost:3000 in a browser it throws an error: "Cannot GET /"
How can I get api.localhost:3000 running? I would consider other solutions, not just ones using express-subdomain as long as you don't have to type in the fqdn in the setup.
app.js:
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
// *** Code examples below go here! ***
var router = express.Router();
//api specific routes
router.get('/', function(req, res) {
res.send('Welcome to our API!');
});
router.get('/users', function(req, res) {
res.json([
{ name: "Brian" }
]);
});
app.use(subdomain('api', router));
app.listen(3000);
package.json:
{
"name": "auth_manager",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"express-subdomain": "^1.0.5"
}
}
I think that the API is alive at http://api.example.com/ and not in the api.localhost:3000. However if you want to run them locally I suggest that you follow https://www.npmjs.com/package/express-subdomain#developing-locally
If you plan to use this middleware while developing locally, you have to ensure that your subdomain is listed in your hosts file. Refer here.
On MacOS:
Adjust host file to forward requests from url:
sudo nano /etc/hosts
127.0.0.1 myapp.dev
127.0.0.1 api.myapp.dev
Clear cache:
sudo killall -HUP mDNSResponder
In express:
app.use(subdomain('api', router));
Any authentication middleware needs to be adjusted to authenticate api.myapp.dev not localhost
In Chrome you can use the api.myapp.dev url
If you are using SSL (https) and have a self signed cert: Chrome usually blocks the hostfile URL if it is not localhost. Then, you need to click anywhere in the Chrome window and literally type in thisisunsafe and it will let you through. Refer to this ticket.

How to deploy node app on openshift and run it?

Just created a very simple hello world app using node:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
app.listen(server_port, server_ip_address, function () {
console.log( "Listening on " + server_ip_address + ", port " + server_port )
});
and it works as it is expected in my local machine,
put that on github and deployed it on openshift, pod created and server running fine:
but when I browse the route which I could find it in Application>>routes menu it says:
Application is not available
The application is currently not serving requests at this endpoint. It may not have been started or is still starting.
I guess I'm using the latest version of openshift since just created an account.
I'm expecting it to show me the Hello world!
update1:
here is my package.json:
{
"name": "npmtest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.2"
}
}
Your application server IP address needs to be 0.0.0.0. So you either specify an environment variable for that, or hard code it.
have you configure main file in package.json
and maybe you need to get port dynamically from the environment variables check below link for openshift sample
https://github.com/openshift/nodejs-ex

App engine nodejs deployment error

I am trying some hands-on on Google Cloud Platform, App Engine in specific.
For the same, I've created a simple nodejs application which just send Hello Wold message in the response.
But I am unable to access the endpoint and getting the below error
below are my files:
aap.yaml
runtime: nodejs
env: flex
index.js
'use strict';
const http = require('http');
const port = 443;
const requestHandler = (request, response) => {
console.log(request.url);
response.end('Hello Node.js Server!');
}
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
package.json
{
"name": "test-pro-for-gcm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"deploy": "gcloud app deploy",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
UPDATE 1
In the GCP log, 16:31:59.000 server is listening on 443
The port that will receive HTTP requests is 8080.
Use the PORT environment variable in your code to make it compatible with the App Engine environment: const port = process.env.PORT || 443;

Resources