deploy node js application on my server - javascript

I'm trying to learn node js. I have a basic "hello world" running on localhost with this code :
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');}).listen(1337, "127.0.0.1");
Now I'm trying to deploy it on a server I got from someone to fiddle around on with node.js installed on.
How do I get "hello world" to appear on my page: thomas.sitestatus.nl? I have checked multiple tutorials and I still haven't find a solution.

Here is some basic steps you can follow to run your application on the server.
1) Copy your project to the server. Install npm modules if needed.
2) Then start the application on some port like 3000 (Later you can use nginx to map it to port 80)
3) After that when you enter thomas.sitestatus.nl:3000 in your browser, you will see "Hello World". If you configure nginx, entering thomas.sitestatus.nl will show "Hello World".
Feel free to ask if you have doubts or questions.

Related

Node.js serves "index of /"

I've been experimenting with Node.js on my windows machine and also my linux machine to create a web server using node.
If I try my code on windows and activate my server with "node server.js" and navigate to localhost:8080 I am met with "Hello World!" as intended.
But if I try on my linux box with the same code it shows me the directory instead of serving the page. Not sure what's going on here!
Here's the code I've been using
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
I've tried using my local ip to the machine and also using the domain followed by ":8080", both give me the same results.
I cant find anybody else with the same problem and I've been looking for hours.
I got the same issue while manually starting server from windows machine.
using command http-server -p 4200
Say, I was trying to start the server from D:/ProjectName. When I changed that and started from D:/ProjectName/build, it was working fine. Build folder has the index file and all other files.

How to set up Node.JS with Express and Socket.IO?

I'm trying to build a Node.JS server to listen on port 3800 of my CentOS server this way:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.send('<h1>Hello World!</h1>');
});
http.listen(3800, function(){
console.log('Listening on port: 3800');
});
I have a domain and I configured this domain on my apache server with virtual host to listen on port 80. But when I try to access
http://example.com:3800 or http://server_ip:3800
it's not working. The browsers keeps trying to connect and then I got the error.
I don't know what I did wrong, since I followed the tutorial. I searched other questions here, I tried to copy the code into my index.js and nothing. This simple "Hello World" is not showing and I can't access the server.
I did the "node index.js" and on my server is showing "Listening on port: 3800" perfectly, I have root access and I did everything with the root user. I did the "npm install express" and "npm install socket.io" commands too, and I tried to make the package.json file and then "npm install". I searched another website and I tried their instructions with "npm install --save: express", won't work too.
I think it's a problem with my Linux configuration.
My question is: how I can make this simple script work when I access http://example.com:3800?
The correct answer to my question is to open the port at the iptables using this command:
iptables -I INPUT -p tcp --dport 3800 -j ACCEPT
And then saving it with the command:
/sbin/service iptables save
CentOS6 based system.
Your actual example will work going to localhost:3800
Im not sure how is that of running nodejs in an apache server :/
if it helps, I normally do it slightly different than your code:
var app = require('express');
var http = require('http');
app.get('/', function(req, res){
res.send('<h1>Hello World!</h1>');
});
app.listen(3800, function(){
console.log('Listening on port: 3800');
});
EDIT-------------------------------
Your main issue here is that you are trying to use nodejs in Apache as I mention above, is not impossible but im sure is a pain to configure your server, and will not be the normal LAMP stack configuration, and you will probably run in some kind of bugs or cutted functionalitys, try to host your app in a node backend, something like openshift or heroku

How to run server written in js with Node.js

I have installed node.js from here http://nodejs.org/ . in my windows8 machine. copied the example server code in my server.js 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/');
then opened the node.js prompt and written node c:/node/server.js
but nothing happens.
I am a php developer just trying hands on it, any guidelines will really be helpful.
You don't need to go in node.js prompt, you just need to use standard command promt and write
node c:/node/server.js
this also works:
node c:\node\server.js
and then in your browser:
http://localhost:1337
Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). To run your code, you need to enter a command in the terminal / shell / command prompt. Look for an application shortcut in your operating system by one of those names.
The command to run in the terminal will be
node server.js
But you will first need to browse in the terminal to the same folder as the file server.js. The syntax for using the terminal varies by operating system, look for its documentation.
I open a text editor, in my case I used Atom. Paste this code
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/');
and save as
helloworld.js
in
c:\xampp\htdocs\myproject
directory.
Next I open node.js commamd prompt enter
cd c:\xampp\htdocs\myproject
next
node helloworld.js
next I open my chrome browser and I type
http://localhost:1337
and there it is.
Just go on that directory of your JS file from cmd and write node jsFile.js or even node jsFile; both will work fine.
Just try
node server
from cmd prompt in that directory
If you are in a Linux container, such as on a Chromebook, you will need to manually browse to your localhost's address. I am aware the newer Chrome OS versions no longer have this problem, but on my Chromebook, I still had to manually browse to the localhost's address for your code to work.
To browse to your locahost's address, type this in command line:
sudo ifconfig
and note the inet address under eth0.
Otherwise, as others have noted, simply type node.js filename and it will work as long as you point the browser to the proper address.
Hope this helps!

Where do I need to save javascript files for Node.js in windows 8

I have JUST downloaded node.js and am having to work with their command line for the first time. It appears that every tutorial on the planet gives the same starter app.
It uses the code...
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
and wants me to save it to the file example.js.
The file can then be run by typing...
node example.js
but all the command line gives me is
...
The tutorials do not say WHERE I should save the file. To my C drive? To the same file as node.js? Anywhere?
I have tried all three and they don't seem to work. If there is any other solution, or simply something else I should be looking into and asking about, that help would be appreciated too. But at this point I honestly have no idea what the problem is, and there appears to be very few resources to help me here.
When you run node example.js from your prompt, it's assuming that example.js is in the current working directory.
To change your current working directory, use the change directory (cd) command:
cd C:\Projects\MyProject
node example.js
This is equivalent to
node C:\Projects\MyProject\example.js
Correct! you can save it any where and change to that directory by the (cd) command on node.js command prompt which will then give message says: "Server running at: 127.0.0.1:8124.
So point your browser to that address and you see your file.

node.js on Apache Linux web server

I was wondering can node.js run with Apache server? My understanding with this language was that js files gets compiled with Google V8 engine but how do we do this?
How do we use this for building web apps?
One way to create http servers in node is the very popular framework express: https://github.com/visionmedia/express
Example Code (from there):
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
I was wondering can it run with Apache server?
Of course. You can run node.js on whatever port you like (subject to the usual limitations) leaving Apache free to have port 80 (or whatever port you like). Apache can easily proxy requests to node if you write your script to communicate over http.
My understanding with this language was that js files gets compiled with Google V8 engine but how do we do this?
From the node.js homepage:
node example.js
How do we use this for building web apps?
There is an example of writing a webserver using node.js on the node.js homepage.
TeaJS runs V8 on Apache, and its pretty easy to use. http://qteajs.org Similar syntax to Node, but with the synchronous programming and Apache. You just include mod_js in httpd.conf, and write JavaScript in .sjs files (instead of PHP or insert your favorite language here)
http://nodejs.org no seriously just read the front page.
They have a pretty clear example.
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/');
You will need to install linux or osx to run nodejs for now.

Categories