What is the main purpose of NodeJS internal HTTP Web Server? - node.js

I'm currently learning NodeJS and pretty excited about it.
But here is one thing that I am unsure and need help about.
As we know that NodeJS has its own internal http web server and we can simply create using http.createServer() method.
My questions are:
Is the internal http web server that NodeJS has, meant to replace similar http web server like Apache or Nginx?
If I need to create enterprise-scale application, does NodeJS internal web service capable of doing this? Or do I need to still use Apache/Nginx?
Please help. Thank you.
Adrian

node.js does not require a web server the likes of Apache or nginx, it is its own web server.
Walmart has used node for, in their words, "end-to-end javascript"... that is certainly enterprise-scale, though they don't explicitly say they're relying on http.createServer, end-to-end javascript would eliminate Apache or nginx and I doubt they've developed some exotic solution (though socket.io is a possibility)... generally speaking if you're using node on the web, you're using the built in web server in at least some capacity.
There perhaps is some benefit to using nginx as a reverse-proxy/static file server on the front end. If I were designing my "perfect system" I'd probably do that, but I'd still be passing back requests to the built in web server in node.

You don't need to use Apache or Nginx with node.js, but in practice, its probably a good idea.
Basically, Apache would sit in front of your node.js, and relay requests to and from your node app, using something like mod_rewrite.
The node http server module is great for firing up quick applications, but if you need things like SSL, complex mod_rewrites, directory security, detailed logs etc (ie all the stuff you would need in a commercial application), you need (or at least, its easier) to use Apache or Nginx in front of node.

Related

MEAN stack project structure doubts

I'm developing a MEAN stack-based application,I've some doubts about the file structure,I'm considering three options:
Frontend(Angular) served by Apache and REST API on NodeJS divided by port(80/443 and 8000)
Frontend(Angular) served by Apache and REST API on NodeJS divided by subdomain(www and api)
Frontend(Angular) and REST API served both by NodeJS divided by routes (/page and /api)
I've searched online, but I haven't found nothing
Personally I'd go with the last option on your list: "Frontend(Angular) and REST API served both by NodeJS divided by routes (/page and /api)".
I saw an answer on here recently that said:
When you serve a node.js app note that you are the author of your own web server. Any potential bug in your app is a directly exploitable bug on the internet. Some people are (justifiably) not comfortable with this.
Adding a layer of Apache or Nginx in front of your node.js app means you have a battle-tested, security-hardened piece of software on the live internet as an interface to your app. It adds a tiny bit of latency (the reverse proxying) but most consider it worth it.
This used to be the standard advice in the early days of node.js. But these days there are also sites and web services that exposes node.js directly to the internet. The http.Server module is now fairly well battle-tested on the internet to be trusted.
Regarding security, this says to me that you shouldn't need things such as Apache or Nginx in front of your Node app.
In terms of opinion of file structure, I'd choose this option also just because I've worked in a number of places where they've done this and it's made interacting with the API simple, predictable and approachable for both internal and external parties who want to consume the API (I'm a big fan of companies using either api.mydomain.com or mydomain.com/api/).
However I guess this down to preference.

Why do some applications need to use other web servers like Apache while node or express doesnt?

I am confused as to why when building a node/express application I dont need to use another web server but when working with Java or Spring or a python backend usually a webserver like nginx or apache is used. I am getting confused as to what Apache and nginx do, don't they just handle HTTP requests just like we do in node or express? But then in Spring there are controllers that handle the requests so why do we need to have JBoss or Apache running?
In the old times there was a strict separation between the "application" and the "application server"/"web server". Application servers (like JBoss) provided the configuration of the resources (connections to DB for example), etc. to the application deployed on them. Web servers (like Apache) provided the configuration for possibly multiple web applications hosted on them.
Currently, in the era of self-hosted apps (which means: apps that contains embedded HTTP server) you often don't need a separate webserver. But tools like Nginx are still used for example as load-balancers, etc. Application servers (JBoss, etc.) are not often used nowadays, because of the embedded HTTP servers that you're able to configure without asking Ops-people to do it for you - it's quicker and more convenient.
If you are writing a NodeJS application then you don't "need" another server, except maybe when you are scaling a production ready deployment
The simple answer is that express, Apache, nginx and JBoss are all web server. Since all of these are web servers they each can pretty much do the work of each other. However, each of them have strengths and weaknesses which is why often times they can work together. For example a common practice is to place an express server behind nginx to let nginx handle load balancing, static assets and SSL termination which nginx is very good at but maybe let API and websocket connections fall through to the express server which is what express is typically good at.
A developer may pick Apache if they are working with PHP because the integration is so good but pick JBoss if they are working with Java EE.

Why is express.js app almost always behind nginx or other web server?

I have seen tons of examples and read tons of articles about nodejs express app deployment. Almost always after implementing all logic (including serving static files) in express, the next step is to forget all the claims about how incredibly fast node.js is and how amazing it is in all benchmarks for concurrent requests. After you no longer remember the reasons why you learnt this amazing new technology which changes the world and how we think of web applications, there you go and install good old nginx to act as an entry gate to your express app.
Don't get me wrong, I understand all the features of nginx, having deployed tons of PHP apps with nginx in my days. But simply put, why? Why not let my app be balanced by pm2 e.g., run it on all cores of my VPS and have node.js native clustering support handle the load balancing?
Obviously, I am talking about the case of using a single machine for the app, not deploying it to multiple VPSs. Then a load balancer of some sort again does make some sense.
Thank you anybody who can explain the reasons for using a web server to forward traffic to another webserver.
You can use nginx server to serve static content of your application instead of nodejs.
As nginx is better at serving static content like html, css, js & image files than nodejs.
And another thing is in case of crashing of any node service, you can show proper error pages using nginx.

We have already had nodejs, why we need nginx or apache?

Recently, I set up me blog site which powered by Ghost -- A light weight, fast and static blog framework. I note that Ghost servers on the nodejs, and I needn't to install apache or nginx anymore.
In this way, why we need apache or nginx? I know nginx is famous of it's outstanding performance, but how about nodejs server's performance?
The V8 engine your NodeJS code runs on is supposed to be a Javascript runtime to execute Javascript code, and not perform as a server.
Therefore, it is better to reverse-proxy your NodeJS application through a server such as Nginx.
Moreover, when you require server based features such as load-balancing, caching, max post size, request timeout etc, it is better to use a proper server software that you can configure these settings on than to depend on the language's runtime. You can still do these things in the language's runtime but that will be an overkill.

Is NodeJS supposed to be standalone (I.E. without apache nginx)

Ok so finally decided I was way behind in knowing some of the frameworks/platforms that are out like Angularjs, NodeJS, Knockout, Backbone etc etc. And so decided to learn NodeJS first, and have set it up on a local vm of ubuntu server.
So was wondering if NodeJS was supposed to be paired with another server software like apache,nginx, etc. And let apache/nginx just serve up the basic pages and then just let node do the data communications since their site says its "for easily building fast, scalable network applications".
Because I have seen several questions on S.O. asking how to get NodeJS to run on port 80, which implies they want to run node as a regular server or they just dont want to have to always specify a port when doing requests. And have not seen anyone comment or say that node was not meant to be used like a regular server. So was hoping to get to an answer on this.
Node.js can be used standalone, out there are good frameworks to do it like express. You can clusterize your process in the same physical machine (and the same port) really easy via its native module cluster. Also, I'm sure you can use Node.js like a reverse proxy too, but some developers prefer using other tools to do it (in my enterprise, we use Nginx with some of our node.js apps).
So, in short: You dont need Nginx or Apache at all, but you can use if you want. It's very cosy to some people use Nginx to do the load balance, or even other stuff like handle the https or server static content. It's your choice at the end.
You should play something with the native library http or https first, and then check express or another framework. You will see wich parts of Node.js you love and which do you dont feel awesome.

Resources