Can we say node.js is a web server?

2020-05-11 00:09发布

I found that I am confusing between web framework and web server.

Apache is a web server.

Tornado is a web server written in Python.

Nginx is a web server written in C

Zend is a web framework in php

Flask/Bottle is a web framework in Python

RoR is a web framework written in Ruby

Express is a web framework written in JS under Node.JS

Can we say node.js is a web server??? I am so confused between web server/ framework.

If somehow node.js is kind of webserver, not webframework (Express does), why do we need to put the whole node.js on top of Nginx server in useful practice?? Question on SO

Who can help???

Kit

8条回答
你好瞎i
2楼-- · 2020-05-11 00:54

No it's a runtime environment... so it is not a web server yet it does not need one to run. So probably this is why it could be confusing. It can run standalone without needing any webserver because it is a runtime itself but again it is not a webserver.

查看更多
别忘想泡老子
3楼-- · 2020-05-11 00:55

How I feel your pain !

Like many, I found it hard to get to the essence of Node.js because most people only write/talk about the part of Node that they find useful - and the part they find interesting is usually a secondary benefit of Node rather than its primary purpose. I must say that I think it's mad for people to say that Node is just a JavaScript runtime. Node's use of JavaScript - and its selection of the V8 runtime - are simply means to an end, the best tools for the problem that Node's developers wanted to solve.

Node's primary purpose was to make the management of user events in a web app more efficient. So Node is overwhelmingly used on the back end of a web app. Event management demands that something is listening at the server machine for these events. So a http server must be set up to route each event to its appropriate handler script. Node uses JavaScript for event handling because JavaScript has callback functions: this allows one task to be suspended until the result of a dependent task is returned. Not many other languages have this feature and those that do may not have as efficient an interpreter as Google's V8 runtime. Most web developers know JavaScript so there's no additional language learning with Node. What's more, having callback functions allows the putting of all user tasks on a single thread without having explicit blocking applied to tasks demanding access to the database or file system. And this is what leads to the superior executional efficiency of Node under heavy concurrent use - which was the primary purpose for its development.

To help Node users quickly write back end code, Node's developers also organized the NPM (Node Package Manager) repositary: this is an open source, user-driven set of script packages for various standard and custom functions. All Node projects allow importation of NPM packages into a project via the established npm install command.

User requests handled via Node will be things like register/login/logout/form field validation and various database queries needed by the web app will be sent to the Node port. Other types of user request, e.g. to display another webpage, download CSS/JS/images, etc, could be sent to Node also. But these are not normally be sent to the Node port but will continue to be sent by the browser to the default port(s) on the server machine where the web server will handle them.

So, in practice, Node is normally a server but one that only replaces some of the functions of the web server program. Other uses of Node simply exploit one or other of its features, e.g. the V8 engine. But these are really just by-product uses of Node.

查看更多
登录 后发表回答