Node - Express -App does not respond to other servers - node.js

On Virtual Machine 1 I have a very simple node script
// server.js
var express = require('express');
var annotations = require('./routes/annotations');
var cors = require('cors');
var app = express();
app.use(cors());
app.options('/annotations/:id', cors());
app.get('/annotations/:id', annotations.findById);
app.listen(80, function() {
console.log('Server running');
});
When I access with the browser or curl from my local machine http://example.com/annotations/5770dffb2dc30a7433c729f7 I get instantly the correct data.
But when I try to it from Virtual Machine 2 with a PHP script (file_get_contents or curl), with CURL or even with w3m I cannot access it. PHP and CURL are trying it for long time and then run finally into timeouts, w3m says
Can't load http://example.com/annotations/5770dffb2dc30a7433c729f7.
I am loosing my mind! Any suggestions?

I suppose the issue is the network, not node. Please check if you can
ping 10.55.55.111
from your 2nd VM, assuming that your first VM's IP address running node is 10.55.55.111. If that fails, you have a network problem.
Two possible solutions:
Your two VMs are on different subnets. A great article about VM network adapters can be found at https://www.vmware.com/support/ws4/doc/network_configure_ws.html.
Maybe your first virtual machine has a "Host-only" network? You might need to set the virtual adapter to "Bridged" instead. Further explanation of "Host-only" and "Bridged" network adapters is given at http://slopjong.de/2013/05/14/virtualbox-make-your-virtual-machine-accessible-from-your-host-system-but-not-from-the-local-network/.

Related

Express.JS server to connect to host on remote network

So I got this small express server running. I can connect it to other devices on my local network e.g. mobile and other PC.
However when connecting over my 4g it does not work. Is there any reason for this? I am sure when I ping other private addresses on remote networks it has worked before, why not now?
Code:
const express = require("express");
const server = express();
const PORT = 3000
server.use(express.static("static"));
server.get("/", (req,res)=>{
res.sendFile(__dirname + "/pages/index.html")
});
server.listen(PORT, "0.0.0.0", (req,res) => {
console.log("Listening on port ", PORT)
});
Any information would be apricated I have some networking experience (still a noob just studying) and this really does interest me.
I assume you are trying to reach the server via your local IP. But you are doing it with 4G (in your phone maybe), which means your request is going over the internet while your local IP is only valid in your network.
Even if you are using your public IP, you would probably have to configure port forwarding on your router for it to know how to handle the incoming traffic for this port.
If you host your express server in your private network , you must have access to the express server by create port forwarding, destination nat, or some kind of publishing private services to public world methods.
If you need more help , i need to know more about your network env , your public ip and ...
Feel free to ask
An have a productive day

Streaming from NodeJS server not working when access from outside

I have a question about streaming an audio file via a NodeJS server. I'm using the following code:
var http = require('http');
var fs = require('fs');
var filePath = 'media/test.mp3';
var stat = fs.statSync(filePath);
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
fs.createReadStream(filePath).pipe(response);
})
.listen(3000);
It does work when I ...
run it locally: http://localhost:3000 or
run it on a different machine in the same network: http://192.168.1.42:3000.
But it does not work when I ...
run it from outside, e.g. calling http://my-public-ip:3000 or
using a DynDNS service: http://my-dyndns-provider.com:3000.
By not working, I mean I can see a pending request ("request is not finished yet!") in Chrome devtools, but the stream sometimes starts only for less than a second, sometimes it doesn't start at all. In the devtools I can see that only 4 KB are loaded (on localhost it's 3.1 MB).
To enable the access from outside, I configured port forwarding on my router, so that requests to port 3000 are forwarded to my computer's internal IP.
For other things than streaming my setup is working, so for example it is possible to call REST routes defined on the server.
EDIT:
Meanwhile, I also tried to do the streaming with PHP instead of NodeJS. But it shows exactly the same behaviour.
Do you guys have an idea what could be the reason?
Thank you!
Looks like some configuration issue with setting up port forwarding in router as you have tried with two different code bases.
If you are doing only for testing you can use localtunnel to expose your localhost to publicly over the internet.
There are few other alternatives also like ngrock or forwardhq.
Hope it helps.
Yesterday, I finally was able to test the exact same setup but with a different router (FritzBox 7320) -- and everything worked as it should. So there must be problems with the router I'm using at home (o2 HomeBox 6441).
I think the best will be trying to get some support in the manufacturer board.
Thanks for your effort anyways!

NodeJs Application on Aws Ubuntu is running on ipv6

While running nodejs with express application on AWS ubuntu 16.04, it's running on tcp6 and due to this, i'm unable to access my application.
see below screenshot.
after adding IP address while creating server, it's giving below error.
I'm new to linux, so I do not know how to resolve it. please suggest.
You need to explicitly provide an IP to bind to in Node.js, otherwise it binds to IPv6. Documented here: https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_listen_port_hostname_backlog_callback
Somewhere in your code you should have something similar to this:
var app = express();
app.listen(1234);
Change it to:
var app = express();
app.listen(1234, '127.0.0.1');

pointing node.js app to my ip address and running locally

So I have two questions about the topic. I have purchased a VPS with digitalocean.com to host my node.js app and they gave me an example hello world app to start.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var html = "hello world";
res.end(html);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
So this code works locally and when I host on the VPS I replace 'localhost' with the IP address of my VPS and it is available on the web when I go to http://IP_Address_of_VPS:8080
My first question: How do I host it live on the web but have it point to my IP address? When I replace 'localhost' with my IP address it does not work when I go to http://my_IP_Address:8080
My second question:
I made a test app following a node.js tutorial and here is the code
var PORT = process.env.PORT || 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.use(express.static(__dirname + '/public'));
http.listen(PORT, function(){
console.log('listening on port *:'+PORT);
});
Where public is the folder with a simple html page. This app works when I run it locally at http://localhost:8080
Can someone explain how the second app runs locally when I did not specify 'localhost' in the http.listen() function? Also how do I point the second app to run from my IP address if the process is any different from the first?
First question
I hope I understood your question right, here is an attempt at an answer :)
Depends a bit on what you mean with 'my ip address'. If it is your public ip adress, you might have to set up port forwarding. Most routers will deny traffic from the outside network (which you are doing when connecting to your own public ip address) to the internal network by default.
If you mean a private ip address (local to the local network) you need to you use your public ip address and set up port fowarding.
When you set up port forwarding to your local machine you might also want to make your private ip address static.
However, I do not recommend using your home computer as a production server. You will need to solve a lot of problems like making your network secure and having low downtime.
Second question
When no hostname is passed to http.listen, the server will accept connections on all adresses. See documentation for more information.
This works, because when you omit arguments in a function call, they will default toundefined. For example, http.listen(8080) might appear as http.listen(8080, undefined, undefined, undefined). Inside the function body these will often be substituted with some default value.
The process of making your app available to the rest of the world should not be any different.

Can't connect to Node web-server hosted on Amazon EC2

I'm running a node app on an Amazon EC2. The app includes a simple web server intended to serve the index page, but it doesn't work.
Here's the server code:
var http = require('http'),
fs = require('fs'),
io = require('socket.io'),
index;
fs.readFile('client.html', function(err, data){
if (err){
throw err;
}
index = data;
});
var server = http.createServer(function(request, response){
response.writeHeader(200,{"Content-Type": "text/html"});
response.write(index);
response.end();
}).listen(1223);
The EC2 is assigned the public IP address 54.187.31.42. I run the app, open my browser, connect to 54.187.31.42:1223 expecting to be served a web page and get nothing.
What is wrong with my server code?
I used the snippet found in this answer to check the IP of the EC2 running the app, and oddly get 172.31.3.67 - why is the returned address different from the one assigned to the machine by Amazon?
Consequently, trying to connect to 172.31.3.67:1223 also fails.
Straight from the Amazon dev controls, if that helps confirm it isn't an issue of the server IP being wrong or something.
The code looks fine, try connecting with the public IP/public DNS that you see in the AWS console.
Try the following and your application would work:
Open the port (in your case 1223) in the security groups of your instance.
stop the firewall on your machine (i.e. iptables) and now access your server using public ip or public DNS.
If you can now acceess your machine that means something in the iptables is filtering your traffic. You can modify the iptables rules accordingly.
In security group, add a rule with the type "Custom TCP Rule" on the used port (e.g. port: 3000 or 1223 for this case). It works for me.

Resources