node.js on Apache Linux web server - javascript

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.

Related

Moving ExpressJS API files onto Server

So I created an simple API using ExpressJS that connects to MongoDB to perform CRUD operations. Currently I am able to get the local host running by performing command "npm nodemon" in the source folder. And it worked by testing with postman I wonder how to implement it on the server. As server runs a linux system, also I have a line of code in my root file "server.js ":
const port = process.env.PORT || 5000;
I think the process.env.port needs need to be changed in order to make it work on the server?
In addition, I did look into aws CE2 server it is so complicated that I was immediately overwhelmed. I am hoping someone can recommend dummy like me a simple and very specific solution to have a server run my scripts in ExpressJS environment. Thank you
I'm assuming your question is "How to deploy express app to a server?"
You can read some advanced topics on http://expressjs.com/, which covers some best practices, and other useful stuff. But the things you want to look at now is Things to do in your environment / setup
The important part is:
Keep your express runing on port 5000
Run your app in cluster
Run your app behind a proxy server like Nginx.
You can check this nice guide (Step 3 and 4) on how to deploy your express app to a Linux server with PM2, Nginx.
So at the end, your express app will run on port 5000 (or whatever port you desire), and your Nginx will run on port 80, and nginx will forward any request to your express app.

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.

Express.js server with Apache Tomcat

I am making a sample web application in node.js using express.js. I am getting a little confused when using apache-tomcat as my web server. I have two doubts:
When i run apache-tomcat as a web server and my express.js server is providing me services at port number 4000, how does it know at which port to listen to tomcat web server. and what if i have multiple instances of tomcat running on localhost, how will it know which instance of the server to connect to.
app.listen(port, "127.0.0.1");
And how does the following line of code works:
var server = app.listen(4000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('server listeninig at http://%s:%s', host, port);
});
Why do we need to take help of apache tomcat or other web servers if we can provide web services using express.js only.
I don't believe that combining express.js (simple web server for Node.JS platform) and Apache Tomcat (Servlet container from JVM world) make sense at all.
I bet that you are confusing Apache Web Server with Apache Tomcat. They are two completely separate projects. If that is the case than notice that Apache Web Server or Nginx HTTP servers are often used with express.js as reverse proxies. This combination is often used to match security and performance (e.g. caching) requirements needed for PROD grade app.
To answer your question, no don't combine express.js with Apache Tomcat. If you are just starting playing with express.js, use it standalone. If it turns to be a app that should run in production, you will need to operationalize it, which would probably include considerations around combining it with some HTTP server.

How do I run a Node.js script in my HTML page, as I do with PHP?

I'm a PHP developer since 2010.
I love PHP, just because it's simple. But I want to learn more about Node.js. It looks interesting, specially because I know JavaScript and I'm a big fan of it.
Does anyone know how do I run a Node.js script in my HTML page, without displaying the source code, as PHP does? Is that way how it really works?
I've seen many tutorials that they execute Node.js in a terminal, but I didn't find a quick way to run Node.js in a simple HTML page.
Thank you! :)
You seem to be conflating two different features of PHP:
Many web servers can be configured to run PHP programs through a PHP interpreter and serve the results to the browser.
PHP is designed as a template language with bells on, so PHP code is embedded in a template.
If you are using Node.js then you will typically:
Write your webserver in Node.js (although you might configure a front end proxy for it). There is an example of this on the Node.js homepage, but there are also various frameworks, such as express, which do a lot of the heavy lifting for you.
Keep your template code separate from your program code. Node has many template modules available for it.
Quoted from the Node.js homepage:
An example: Webserver
This simple web server written in Node responds
with "Hello World" for every request.
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/');
To run the server, put the code into a file example.js and execute it
with the node program from the command line:
% node example.js
Server running at http://127.0.0.1:1337/
Here is an example of a simple TCP server which listens on port 1337
and echoes whatever you send it:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
The php code that you have been writing likely is only the html template version of php (for lack of better terms...)
When a .php page is requested in the browser, a php interpreter is invoked that parses the php tags in the html and replaces it with html/text. That result is then sent to the browser.
node.js doesn't work that way.
Node.js is a lot more.... verbose than php when it comes to this specific topic. node.js is FAR more than just a web application framework or webserver, it can also be used as an executable of sorts to run common tasks.
typically, to get the kind of functionality you are looking for in node.js, you would use a templating framework such as handlebars along with express to handle the webserver and routing. Here's an example:
// this is just an example, it may or may not work, I did not test it.
var express = require('express'),
app = express(),
exphbs = require('express-handlebars'),
hbs,
path = require('path');
// serve all files under the /assets folder as static files
app.use('/assets', express.static(path.join(__dirname, '/assets')));
// handlebar engine config
hbs = exphbs.create({
defaultLayout: 'main'
});
// attach engine and specify view location
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, '/views'));
// home page http://domain.com/
app.get('/', function (req, resp) {
resp.render('home', {title: 'Home | Hello World!', text: 'Welcome to my site!'});
});
// start webserver
app.listen(3000);
the above node app would create a webserver listening on port 3000 that responds to requests to /assets and to /. When / is requested, the home.handlebars view from the /views folder would be rendered using the main.handlebars layout from the /views/layouts Here's an example view that would display the title that was passed in for the / route created above:
/views/layouts/main.handlebars
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>{{title}}</title>
</head>
<body>
{{{body}}}
</body>
</html>
/views/home.handlebars
<h1>Hello World!</h1>
<p>{{text}}</p>

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!

Categories