why can't other person access node.js server that i made? - javascript

i made a server with node.js like below:
// basicServer.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
and it worked in my Mac and i can access. But, my parallel window can not access.
Of course, other in other computers, too.
Should i make a server with apache or NGINX?
Could you explain why other computers can not access my node js server?

Configure your application to run on 0.0.0.0:
// basicServer.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World');
}).listen(1337, '0.0.0.0');
Now you can use your IP to access.

If you are on the same private network they should be able to access using your private IP. For example http://192.168.1.33:1337/.
If you want them to access from outside you'll have to map the 1337 port to your private server IP.

Related

socket.io connection fails when node.js deployed to Google App Engine

Here's a slice of the code I can run locally just fine:
Server:
var app = require('express')();
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.get('/', function (req, res) {
console.log("Got '/'");
res.status(200).send('Hello, world!');
});
var server = app.listen(process.env.PORT || '80', function () {
console.log('App listening on port %s', server.address().port);
});
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log("connection");
});
Client: (TypeScript, run from a separate app on a different port, http://localhost:8080)
this.socket = io("http://localhost");
this.socket.on("connect", () => this.onSocketConnect());
When the server si deployed on Google App Engine, and the client is run locally with the address changed to http://my-app-id.appspot.com I get the following error:
WebSocket connection to 'ws://my-app-id.appspot.com/socket.io/?EIO=3&transport=websocket&sid=-Qr3YEeyZLk9K3zmAAAC' failed: Error during WebSocket handshake: Unexpected response code: 400
What's causing this and how can I fix it? Also, once I get this running, I plan to change to https, anything I need to be careful of there?
At this time, connections to App Engine applications over the ws or wss protocols is not supported. It is possible to get the IP of instance serving the initial requests and then connect to that instance directly but this circumvents App Engine's load balancer which sort of defeats the purpose of deploying your application to App Engine in the first place.
There is currently an open issue (Issue 2535: Web Sockets API) on the App Engine public issue tracker. Feel free to star this issue to support it and receive updates.

Can't access node http server from external IP

I've node application which work fine on my localhost, but whenever I try to access it from external IP, it doesn't work.
This is my code:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('My node server is working!!');
}).listen(3000);
Any help is apreciated,
Thank's in advance

Deploying nodejs app on a web server

I am new to NodeJS and learning, I want to know how to make an sample app using NodeJS.
I have tried it on localhost and it is working. Now I am trying to host it public to any server, but it is not working.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1/');
This is code is saved in my local machine as m.js, I run:
$ node m.js
It runs fine and when i open it in browser as https://127.0.0.1:1337
I am getting the output as :
Hello World
I want the same thing to do from a remote server i.e. I have a domain www.example.com and if I open it with https://www.example.com:1337
then it should show on my browser screen like previously :
Hello World
But it is not working.
#Annanta 127.0.0.1:1337 is a local machine ip address. please remove this.
Please find below the sample code. Try to access it with remote server ipaddress. Please note the remote machine must have node and npm installed.
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(1337);
console.log('Server running');
Here is a possible work-around using an Apache2-Webserver:
Just edit the Virtual Host in your conf.d (for Ubuntu you´ll find it in /etc/apache/), run a2enmod proxy and restart the Apache.
Here is a possible configuration:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName your-domain.com
ServerAlias www.your-domain.com
ProxyRequests off
ProxyPass / http://127.0.0.1:1337/
ProxyPassReverse / http:/127.0.0.1:1337/
</VirtualHost>
Its actually really simple. Just don't use any IP at all and just define the port.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
console.log('Server running!');
Also as Sach stated you can't just upload it to a webserver. You have to have node and npm installed and you have to actually run the code via
$ nodejs application.js

Node JS - Server doesn't react to requests

I set up a Node JS server, and made a request to it, it just loads and loads and eventually says "Server not found". Here is the code for my file:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
When going to externalIP:1337, the phenomenon described above happens. I am running Ubuntu 14.04, node JS version 0.10.32.
What is going on?
You're specifically listening to 127.0.0.1 which is localhost. If you want to allow connection via the external IP, you should omit the '127.0.0.1' argument in your listen. i.e. change listen(1337, '127.0.0.1') to listen(1337). Otherwise go to localhost:1337 instead.
The problem is that you're only listening for requests on localhost. If you try to access the server from outside the system you won't get there because the server isn't listening on a LAN IP.
Change
.listen(1337, '127.0.0.1');
to
.listen(1337);
That will listen on all available network interfaces on the system. You could specify a LAN IP (just like you did for localhost) if you wanted to listen on a specific network interface.
Sorry.
Apparently tomcat was also using port 80. So by disabling tomcat I got it to work.
Thanks.

Node.js on a remote server: io is not defined

I'm building an application that links to a node.js hosted on my home computer (78.233.79.103:8000)
The server is properly istalled (if you go to the addres I gave you'll se the socket.io works)
If I run the server in localhost with
node server.js
all is good, the application works
Then when I run the application on my other pc or on my iPad (I wrapped it with phoneGap so it's just a web app included in a native iOS app), trying to connect the io on 78.233.79.103:8000 i got the console log:
io is not defined
here is my sourcecode: https://github.com/synbioz/puissance4
look for the server.js
I know I only call io = require('socket.io').listen(PORT); but kwnow also I should create something like:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, '78.233.79.103');
that actually doesn't works
Any idea?
probably because .listen() doesn't return this.
try writing them in separate lines.
var io = require('socket.io');
io.listen(PORT);

Categories

Resources