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
Related
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.
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
im making a mobile app that require a http server so im trying to make it with browserify so this is what im doing, i got this server.js
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/');
then i do this
$ browserify server.js > browserify.js
then i enmbeded to my html like this
<html>
<body>
<script src="https.js"></script>
</body>
</html>
then when i run the html i get this error
Uncaught TypeError: http.createServer is not a function
how can i get a http server working on the browser ?
by the way i got http-browserify installed
Apologies if I'm reading this question wrong, but you can't create an http server from within a browser. That http.createServer call is only meant to be called (and is only defined) in node.js.
So you would need to run your script from node from the command line, like this:
node https.js
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.
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);