send https request nodejs express from another computer - node.js

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

Related

How to connect to node.js server from any ip

I want to create a private backend for an application I want to make, but I am having trouble connecting to my node server, I have the basic stuff right now,
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 300.')
But this only works for https://localhost:3000/, how do I make it so that if I have a separate computer on a separate connection, I can connect to this server?
I am not interested in just opening it to everyone but just to specific client IP's...
If the client IP's are on the same network as you, you can check out this question
If you want people from anywhere to access your application, I suggest hosting it on something like Heroku (very easy to deploy, pretty good free tier). You can then create a whitelist of IPs in your express application.
I would suggest for any port forwarding using ngrok or configuration in your router
For downloading ngrok https://ngrok.com/ go to this link
For configuration your router it will take some searching in google based on what type of router your using
You must mention that your localhost or Nat Ip and your public IP to resolve here is NOIP refrence https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
As you specified that you want the backend to be private, such that it can only be accessed by your specified node. You will have to host this node server on a cloud service or you can host it on your local machine by opening a port for the node server. Suppose you host the node server on port 1234 on your local machine ip address.
You can start the node server on localhost and your desired port, but you need to allow requests to the particular port.
Now you need to check for the origin of the request that your node server receives and validate that, so that only your private node(computer) can access the node server. You can do that by validating the hostname using express, you can get the hostname of the request in express.js using req.hostname or req.headers.host.
You would need to use express.js for this functionality and the code would be as follows
let express = require('express');
let app = express();
let allowedHost = 134.32.234.3 // the hostname which is allowed to access the backend
let port = 1234; // desired port
let host = 0.0.0.0 // desired host; 0.0.0.0 to host on your ip
app.get((req, res) => {
res.header('Content-Type', 'text/html');
if(req.hostname == allowedHost){
res.send('<html><body><h1>Hello World</h1></body></html>');
}
else{
res.send('Connection not allowed');
}
});
app.listen(host, port, ()=>{
console.log(`The server is running on http://${host}:${port}`);
}

Express server causes error "This site can’t provide a secure connection localhost sent an invalid response"

I'm trying to make a simple hello world kind of server using Express.
Here goes my code:
const express = require("express");
const app = express();
app.get("/",function(request, response){//what to do when someone make get request to the homepage or route
console.log(request);
response.send("hello");
});
app.listen(3000, function(){
console.log("server is listening at port 3000");
});
When I run the program I see this at the command prompt:
server is listening at port 3000
But when I'm accessing it through a browser i.e. https://localhost:3000, I'm getting an error:
This site can’t provide a secure connection localhost sent an invalid
response. Try running Windows Network Diagnostics.
ERR_SSL_PROTOCOL_ERROR
I expect the browser to see hello as per my method, above, response.send("hello")
You need to access it through the HTTP protocol instead. try to connect to http://localhost:3000 instead of https://localhost:3000.
Basically what app.listen does is that it creates an http server and listens on it, so you have to use http protocol to access it and not https.
This is the source code for app listen taken from express:
app.listen

localhost:5012 Cannot GET /

I've started up a new server, running it on port 5012, and it's showing Connected to MongoDB. But when I try the port number like this localhost://5012 on my browser, it displays Cannot GET /
Your app is running locally on port 5012.
You should be able to access http://localhost:5012/api/items but not just http://localhost:5012 because your express server doesn't have route handler for '/' route.

Making my Node app accessible to the Internet from a local machine

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

nodejs express local ip address

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.).

Resources