Node.js for a website - node.js

Can I use node.js to build a web server and domain from scratch, instead of using apache for a website? Can All the backend hosting and domain work be done by node.js?
Thank in advanced, I'm trying to build a site for my resumé, would like to build it from scratch, to showcase my resumé. Back to front end. ...etc

So, sort of yes, but also sort of no. To build a back end with node you need to use a framework like express, and to serve it you'd need something like this. But to safely expose it to the open internet, you should definitely use apache or nginx to proxy requests to the port that your app is listening on. This is because it's extremely unsafe to expose your node server to the internet, and because both ports 80 and 443 require root privilege, making it even more unsafe. But if you really want to serve directly to your js backend, you can do so with something like this Best practices when running Node.js with port 80 (Ubuntu / Linode) for linux or this https://unix.stackexchange.com/questions/319734/pf-forwarding-all-packets-on-port-80-from-any-interface-to-socks-proxy for mac. This also requires root privilege to implement, and is still extremely unsafe.

Related

Serve nodejs app from nginx like with php?

I use Nginx to serve my php applications for dev purposes.
On Ubuntu it works out of the box.
I want to do the same for Node.js apps.
Is this possible without doing nodejs app.js before?
How to achieve this in a single Nginx conf file?
PHP and node.js are oil and water. PHP requires a web server to run the .php files, however node.js typically creates its own web server. Since you are creating your own web server, in many cases you wouldn't find it necessary to serve your application from Nginx, however, if you truly insisted on "serving" it from Nginx, you would need to proxy it.
This is not possible without doing nodejs app.js before, due to the way node.js works.
This question best answers your question regarding proxy'ing via Nginx.
As a closing remark, its good to remember that node.js does in fact (in most cases) implement its own web server, and PHP does not.

Where to run node.js

So I thought about giving node.js a try seeing the possibilities it has for a little test chat project (with mysql) I'm doing.
But what I couldn't find out is where to run the file from and whats most common.
What I currently have:
A FreeBSD server with latest Node and PHP 5.3.x
A vhost
some tutorials on how to start with node (which I looked through and got exited about)
knowledge on how to run it from terminal without having to keep my terminal open (screen)
So far so good.
What I need:
Some basic information of where to put the (lets say:) chat.js file.
Most logical port to run it on
So the web root (www) runs on a user (not root obviously). And the webroot has an underlying folder where I could put the script (away from visitors grabby little hands). This seems to me to be the most safe place to put it seeing nobody can get to it, which is probably what I want seeing I'm going to connect to a db and don't want my DB login data out there (I don't know how this works yet but I'll figure out db connect with node later, no answer required).
But if a file is not in the webroot, it seems to me a connection cannot be made from outside. Cause my webroot is configured to only allow 80 (or ssl on 443) incomming traffic, which is logical. Outgoing obviously has no problems.
All the examples that I found don't really help me. They just do everything on a local machine, which sucks for me cause I don't want to do that.
So what I would like to is the best practice for:
Where to put the file
port to run it on.
What is Node.js?
A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. The description on nodejs.org definitely doesn't help.
An important thing to realize is that Node is not a webserver. By itself it doesn't do anything. It doesn't work like Apache. There is no config file where you point it to you HTML files. If you want it to be a HTTP server, you have to write an HTTP server (with the help of its built-in libraries). Node.js is just another way to execute code on your computer. It is simply a JavaScript runtime.
A nice tutorial How to Deploy Node JS Applications, With Examples
You'll need to have your non-node application on port 9000 (for
Apache, this will be in /etc/apache2/ports.conf and in your
sites-available file for your site), and you'll need your node
application to listen on 8080. You'll also need to set up DNS 'A'
records for the different hostnames you'll be using for your servers.
Companies like Heroku allow for automated deployment of apps from the desktop to the cloud.
Nodejitsu provides a tool called jitsu that makes deploying an Node.js application super simple. You can install jitsu with npm.
npm install jitsu -g
Heroku How To
Getting started with jitsu
Use monit, forever, upstart or systemd to start your node server. Use Varnish or HAProxy or Nginx (Nginx not work with websockets).
Ultimately you can stick it anywhere you want. I recommend running your application using Forever or similar instead of directly with Node. I usually keep my apps in /var/ and let each one run under a unique user. I do not recommend keeping them in your http root, as your .js files are should NOT be interpreted by Apache, php, etc.
To be clear - you do NOT need a traditional webserver, nor do you need php,mySQL or anything else. Node is all you need. It'll serve content directly - it IS the webserver.
Often times you'll have each app use a high port number (3000+) and use NGINX to proxy them all to different hostnames off of port 80 (allowing you to easily have multiple apps on a single machine). If you aren't using some sort of proxy, then 3000 is very default, but there is no correct or incorrect port, so long as you don't use a reserved port.

node.js socket.io apache server

I have some sort of noob question regarding to node.js and socket.io. These two guys are usually needed to use websockets for server to browser communication. Once that my project gets done, do I have to upload my php files to my apache server. Now my question is do I have also to upload my node_modules directory to my apache server? along with socket.io directory?
In order to run your node.js process you need to have ability to execute own applications on server. Classic virtual server does not allow of such functionality. There is virtual server hosting for node.js specifically, check out nodejitsu, heroku or any other.
You can run node.js as web platform by it self, without of use of any apache, that will make your life much easier. But if you still need apache and PHP, then you need to consider proxying from apache to your node.js process. This still can be tricky as proxying WebSockets is not straight forward.
If you have access to Apache settings, then you can enable proxying for HTTP/HTTPS, read this: http://ronenagranat.blogspot.co.uk/2011/02/apache2-reverse-proxy-for-nodejs.html
For WebSockets, there is not much data available but here is possible solution: http://blog.cafarelli.fr/post/2013/04/26/Backporting-Apache-support-for-websockets-reverse-proxy-(aka-getting-GateOne-to-work-behind-Apache)
Although if you have own server and have ability to choose between web platform, I would recommend to look into nginx, as it has support for WebSockets proxying (might require module and nginx recompiling).

Node equivalent to index.html

I'm coming from a LAMP background and want to test out node for production.
What seems a little confusing to me is that in Apache url's map to folders, and the server will automatically look for an index.html or index.php if you aren't re-writing urls.
What would the equivalent be in node?
I'm thinking it'd be something like checking the request url and matching it, then loading a specific node module which runs the app.
This might seems simple for a single app, but we run tons of client apps on our server so i'm used to having different frameworks in different folders and index.php just runs it.
to be more specific. i'm currently running a few codeigniter and wordpress installations on our server. so i'd want to run a few node apps/frameworkds in different 'subfolders'
With Node you don't really use Apache. It's similar to Ruby in that is runs its own web server.
However, you can probably get Apache to run Node files using mod_node. As far as I know this is non-standard though and you definitely lose the "non-blocking" advantages of Node. But for experimenting (and not load testing) it's fine.
Check out Express if you're looking for an MVC architecture written in Node.
If you're just looking to run the most basic sample web server, just run the example hosted on the main page: http://nodejs.org/
Lastly, I had that same issue you are experiencing where I have one box hosting a lost of stuff and Apache taking up port 80. The answer here is to use a reverse proxy like Nginx to run on port 80 and redirect traffic to Apache / Node / Ruby / etc. Best of both worlds and since Nginx is written non-blocking you still gain the benefits of node.
I actually wrote an in-depth blog article about this a few months ago:
http://readystate4.com/2011/07/15/nginx-apache-and-node-all-living-harmony/

Is it good idea to put NodeJs behind nginx

Is it good Idea to put nodeJs behind nginx , also can someone let me know nginx supports http 1.1;
Also how to make sure websockets works with this setup ( nodeJs Behind nginx)
If you want WebSockets, don't put it behind nginx. There might be some way that I don't know of, but DotCloud doesn't support WebSockets with Node.js because of its reliance on nginx, and they know nginx pretty well.
I assume you want to run your server on Port 80. If node is your main server, that will mean either:
Running node as root. This is often not ideal because there is potential for bugs in app code and with root access it could cause more damage. If a VM is set aside for a very particular purpose, all backups are made to outside of the VM, and rebuilding is quick, this may not be a big problem, though.
Using iptables to forward network traffic to port 80 to a higher-numbered port. I set this up and I felt like it was a good solution.
Edit: You can also run node.js as root and downgrade to a non-root user with setuid after binding to Port 80. The Jetty project (a web server for Java) suggests this technique.
nginx doesn't fully support HTTP 1.1. However, work is being done and will possibly be integrated into the development branch soon so keep your digits crossed and have a look at this mailing list thread to see what I'm talking about (there are patches but I haven't tried them as yet). More discussion here.
Depending on your needs you can do what Ben suggests using IPTables although I would also 'stealth' the high port using the mark module; I've upped a simple shell script that will do it for you.
If you need other applications on 80 you'll need to proxy; haproxy is one option but you can keep it all node using the excellent node-http-proxy.

Resources