sending http request from azure web app to my machine - node.js

I created an azure web app which send an http request:
axios.post('http://*mypublicip*:3000/write/' + Name, {
content: data[1]
})
meanwhile my computer has a running express server:
app.listen(3000, () => console.log(`Server running on port ${port}`));
all my functionality works when I use it locally by sending the request to localhost instead of my ip.
I also tried adding host '0.0.0.0' to my app.listen.
what am I missing? thanks

This problem has nothing to do with your webapp.
So I will not test the webapp program anymore, you can access your http request of http://*mypublicip*:3000/write/' + Name through other machines (not PCs in the LAN) , My test environment is vm built in azure. You will find that your request cannot be accessed successfully.
The reason is that the ip you found in ipconfig in cmd should not be your host ip, but the Internet ip assigned to you by the operator. This ip is used by many hosts. Simply put, this ip is not just one of yours Machines.
So cause your problem to happen. It is recommended to use intranet penetration tools to achieve this. It means to use the intranet penetration tool to expose your 3000 port.
To solve your problem, I used ngrok.
Build the project locally and monitor port 3000.
Local visit, check the effect.
Access in the virtual machine, discovery failed.
After installing ngrok, the virtual machine accesses the new address and it succeeds.
First allow port 3000 to be exposed.
Test in vm, it works.

Related

How do you host a Node server from your computer so everyone can connect to it

I've made a node program and I want to host it trough my computer, how can I go about doing so. Currently I'm using "require("net");" function to start the server locally!
var net = require("net");
var server = net.createServer();
server.on("connection", function(socket){
//Stuff happens in here
});
server.listen(process.env.PORT || 6969, function(){
console.log("Server is listening to %j",server.address());
});
To run a Node.js application you have to install Node.js - you can download it HERE. Then in the command line / terminal, you navigate to the location of your code, and run Node.js with your file as an argument:
cd /c/some/location/with/your/file
node file.js
There is now a Node.js process that is listening on your chosen port - so it's hosted locally.
Looking at the Node.js documentation for Server.listen, it looks like the app is already listening for outside connections. The signature (or one of the signatures) is server.listen(PORT, HOST);. And I also see:
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
So your app should already be contactable from the outside (although you may have to open the port explicitly depending on firewall rules). So some other computer, if they make a TCP/or whatever request to <the IP address of your computer>:6969 then the server will respond.
I'm actually surprised that the default host is 0.0.0.0 and not the loopback address (localhost or 127.0.0.1 - these are the same address).
Note not my answer look it up here:
https://serverfault.com/questions/271824/node-js-is-not-accessible-from-external-ips-on-ubuntu
You cant access node.js from outsiede because it is listening on localhost (127.0.0.1). You need to configure it to listen on 0.0.0.0, with this it will be able to accept connections on all the IPs of your machine.
server.listen({
host: '0.0.0.0',
port: 6969
});
If i understood your question correctly, you want to host your node app with your computer as a web server. It's pretty easy you can follow this blog.
Basically the steps include:
Getting a Domain name.
Registering your IP with DNS so that your
domain name points to your computer when someone enters that in the
browser.
Running the web server (Node) to handle the requests.
Note: You might face some problems if your ISP(Internet Service
Provider) changes your IP address frequently.
Since you clarified your question is about how can you host your application so other people can use it, it's not really Node-specific.
You have to open the appropriate ports in your system-level firewall and in your router as well (search for your router's model and you'll see it). Web applications always run in port 80 (for HTTP) and port 443 (for HTTPS), both in TCP. Do note that some providers won't allow you to host a server in port 80, so you'll have to try it out to check if that's the case for you.
Your Node application seems to be running in port 6969, so you either set it to use port 80 (which requires admin privileges) or use a reverse proxy to send all traffic to/from port 80 to port 6969. You can do that with nginx, for example.

how to connect to a server running on computer using computer's IP address?

I am working on IOT project in which I have to change some variables(fans speed, lights, etc). So just as a starter, I created a node.js server and tried to send requests to the server through a local network using local IP as
http://localhost:7000/users=mandar?lights=OFF
or
http://192.168.43.248:7000/users=mandar?lights=OFF
and it works fine.
Now I want to do the same over the internet. So I got Computer's IP address from https://www.google.co.in/search?q=myip and tried to send a request to the following URL:
http://(IP_address):7000/users=mandar?lights=OFF
This time it keeps on loading and finally shows this site can't be loaded.
So what is the right way to connect to the server through the internet?
Thank you.
You have to do port forwarding.
The IP address you get from the google search is the out-facing IP address of your router. However, your router knows your computer by your local IP address (i.e. 192.168.x.y).
You have to configure your router to send packets coming from internet destined to port 7000(or any other port) to your computer's port 7000.
Check your router's documentation on port forwarding. Likely there is a settings page on the web interface of your router that you can do the desired port forwarding. After configuring the router, there are several tools online to test if the port forwarding is actually working. I suggest you use one of those tools to verify the configuration before testing with your project.
You also might want to check if your router has a firewall. You can add an exception to the firewall such that a specific port number is reachable from the internet.

How do ports work in Node.js when on the server?

I'm currently developing a node server locally, and to access it I have the server listen on 8080 and then go to localhost:8080 in the browser. However, how will ports work when I put this project on the internet?
How it works once you "put it on the internet" will be exactly the same. The browser still makes a request to a port (most likely 80 or 443 on the internet), except it will likely be going to a webserver via the internet instead of localhost.
If you want to learn about deploying a node project to a server check out this guide. Basically you need to setup a proxy to sit in front of node to handle all the server stuff like requests and ssl certificates.
I am assuming you mean serve your node.js program and proxy your localhost port to Internet by put this project on the internet. A short answer is that you never host your API on some port on Internet but exposure your local port to a Internet port.
I believe you already know someway to serve a node.js program stably. If not, I really recommend pm2.
If you want to exposure your API to Internet, you first have to get an IP for your local network so that other people can reach your network by this IP. You can also relate it to a domain. Then use softwares like IPfire to manage port mapping. For example, you can map yourIP:8080 to IP ofLocalhostAtLocalNetwork:8080. Then, people can reach your API through yourIP:8080. Or you can simply map yourIP:80 to IPofLocalhostAtLocalNetwork:8080 so that people can reach your API directly by your IP.
Another very popular tool to server and proxy services is nginx. With Nginx, you can easily serve some services and proxy whatever port to whatever service.
You will need to access the global variable "process.env" to dynamically determine the port number when your code is running from a server. The process.env property returns an object containing the user environment.
You may do something like the following to cater to both local and server environments dynamically:
//set port and ip
app.set("port", process.env.PORT || 8080);
app.set("ip", process.env.IP || "0.0.0.0");
app.listen(app.get("port"), app.get("ip"), function (err) {
if (err) {
console.error(err);
return;
} else {
console.info("cicak is running on port " + app.get("port") + ".");
}
});

Port forward not working to set a local web server

I have just created a simple web server using node server and it's running fine. I can access it from the same PC by going to address http://127.0.0.1:1337.
Now I want to access that web server from my WAN IP. I got my my using whatismyip and got something like 110.36.xxx.xxx.
When I tried http://110.36.xxx.xxx:1337, I got:
Firefox can't establish a connection to the server at 110.36.xxx.xxx:1337.
Here is the screenshot how I created the port forwarding in my router:
What's wrong here?
Localhost is only accessible from the same pc. You have to launch your webserver either on address 0.0.0.0 (it will be available on all network interfaces) or 192.186.0.5 so that it is accessible from your wan interface

Port Forwarding on website

I'm noob in this field, so please help me understand this:
I have my web application launched on port 8080. I thought that if i forward port 8080 and enter from web browser: myexternalipadress:8080/Index.html it should open my website. please tell me why i'm wrong
Ahh, hosting a website from your house is a fun thing indeed. In my experience, here is how I approach trouble shooting.
1.Making sure your server is configured properly.
Launch the server application
If your client and server are the same machine, make sure you can reach the server on localhost
Access the server from a different computer on your LAN, use your servers lan ip. 192.168.?.?:8080
If you can't get to it from another machine on your LAN, you may have firewall issues on the server
2.Making sure your network is configured properly
This is where port forwarding comes into play. Figure out the LAN ip of the server and log into the router.
Tell your router to forward the port (8080 in this case) to the server LAN ip address.
Test it by telling your friend to access your server on (WAN_IP:8080 in web browser)

Resources