Deploying nodejs app on a web server - javascript

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

Related

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

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.

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

node.js http opening static textfiles in browser

If i have a httpserver in node.js that is serving a website on port 3000.
var httpserver = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function(req, res) {
res.render('index.html');
});
I need to access a txt file located at /public/
If i for example do localhost:3000/other_site.html, if this other_site.html is in /public/ it opens it, but if the file is txt it doesn't open it.
Is there a solution to this without having to install http-request, connect or other npm modules?
Regards,
Can you use express? If yes,
app.use(express.static(__dirname + '/public'))
makes the trick.
If not, try searching the archives :) Using node.js as a simple web server
What error you are getting ?
if you experiencing browser error, try sending response header and set Content Type text/html and retry
Example code
fs.readFile('myfile.txt', function(error, file) {
response.writeHead(200, {'content-type':'text/html'});
response.end();
});

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