I have a NodeJS app running with express which I'm trying to access at the port 80.
So I have this:
app.listen('80', function () {
console.log('Server started');
});
Going to the browser I can acess it by typing "localhost", "127.0.0.1", both with or without :80 as a port.
My question is how can I access it from another computer?
Whenever I type the IP on the browser, it respond as "bad request, invalid hostname"
Your approach is correct. The Express application should be visible by other computers on the same network.
My best guess is that there is "something" running on your computer that prevents port 80 to expose. Or a conflict with another application that is using port 80 as well. (most unlikely because express cannot run if the port is already in use.).
Related
I have a NodeJS micro-service hosted on Google Cloud Run.
The service houses both an express server for user facing routes and a gRPC server for communicating with other internal micro-services.
Below is my code so far:
....
//For the express server I am using an hard coded port because of port conflict with grpc server port
const app = express();
const hardCodedPort=3000; //I can't use process.env.PORT here because grpc server below needs it
app.listen(hardCodedPort);
......
//For grpc I am NOT using an hardcoded port, I am listening to the port provided by Cloud Run as shown below
gRPCServer.bindAsync(`0.0.0.0:${process.env.PORT}`, grpc.ServerCredentials.createInsecure(), () => {
gRPCServer.start();
});
Now, owing to the fact that Cloud Run can spin very many instances of the above service will I hit a port conflict for my express server since it is always listening on an hard coded port?
The Cloud Run Frontend (GFE) only supports two ports: 80 and 443. If a client connects to port 80, the client will be redirected to port 443. In other words, your Cloud Run service can only support one internal port number, which defaults to 8080 (configurable). Your application can only listen for connections on one port.
how can i post / get data from nodejs server using another computer ?
so i'm connecting too computers using ethernet cable ,and i was able to open the website (react ) but
when i try to post or get data i get this error ( from the one who's not running the server on )
POST http://localhost:8080 net::ERR_CONNECTION_REFUSED
the too computer are running on the same network
i replaced localhost with the local IP address but still same error
i also added my local ip adresse to the app.listen
app.listen(PORT,'169.254.xxxx', () => {
console.log(`Server is running on port ${PORT}.`); //PORT = 8080
});
but still getting same problem
I created an hapi.js backend app on heroku. After a bunch of problems all works well. Now, I want to create a frontend app with react.js, but I have a problem:
const server = Hapi.server({
port: process.env.PORT,
host: '0.0.0.0'
});
To define the port of the backend I've the enviroment variable, so I don't really know its value. So how can the react app knows the correct port of the server where to connect?
You actually don't need to know the port number. You can use the default port which is 80 for HTTP and 443 for HTTPS.
According to the heroku docs:
The contract with Heroku is for the process to bind to a port to serve requests. Heroku’s routers are then responsible for directing HTTP requests to the process on the right port.
Which means heroku listens to port 80 for HTTP and port 443 for HTTPS by itself.
References:
https://devcenter.heroku.com/articles/runtime-principles#web-servers
https://stackoverflow.com/a/51572239/5045878
https://stackoverflow.com/a/54200996/5045878
I would say you don't need to know the port because all your requests will be done to https://hapi.yourdomain.com, and your front will be served to https://yourdomain.com
Edit:
In heroku this is how you define the port:
const port = process.env.PORT || 8000
Heroku will provide the environment variable you need (that changes every time you deploy your app)
Then your react app don't need to connect to a specific port, just access your endpoints like this :
https://back.domain.com/your/endpoint
OK so I know I can use cloud hosting and I've done so before but I am doing a demo and I want my node app to be on my local machine but accessible from the internet. Here is how I start the server in the server file
const port = 8080;
var server = http.createServer(app).listen(port, () => {
console.log(`Server listening on port ${port}`);
});
Next I set up port forwarding on my Xfinity gateway such that both port 80 and port 8080 point to my desktop. I know I am connecting to the right device because SSH works from outside of my network on port 22. However when I enter [public IP]:8080 I am unable to receive a response. The only time I receive a response is when I enter 10.0.0.58:8080 which refers to my internal network. Why is this???
I personally like ngrok to do the same thing. It's really easy to setup and I found it really stable.
Give it a go https://ngrok.com
After installing you can simply forward ports like
ngrok http 8080
It's fairly straight forward to get the hostname and URL from a request that has been made to an express server. But there seems to be no obvious way to determine which URL an express server is listening on (after it has been started up, but before any requests have been sent).
How do you find out which host and port an express server is listening on? Does express even know which host and port it is listening on?
If you create an server like this:
var server = http.createServer(app);
The server instance actually contains the port and address it's listening. So you could just export it, and call server.address() to get the address and port.
{ address: '::', family: 'IPv6', port: 3200 }
If you want further info about what urls are your routes are routing, try
console.log(router.stack)
then the magic happens. :)