node. JS usage sentiment

Source: Internet
Author: User

Recently tried to use node. js, but because not too deep (small project, not finished), so can not talk about experience, to talk about the use of feelings.

Unlike a single role in the server +cgi program (such as Apache+phpmod) of node. JS and the previous CGI interface, he is a complete program that provides a variety of features in the form of a library, and if you do not write a line of code, Apache, Nginx and so on at least can be a file server, and node. JS even port is not listening. As you can see, node. js is a lua/python-like interpreter JavaScript interpreter that extends the JavaScript library to features such as sockets, files, binary buffer, and so on. As you can see, node. js can only be used as a server, not just a server. Let's talk about the pros and cons of node. js I think.

Advantages:

    1. node. js, with JavaScript as the only supporting language, is his hallmark and the reason why he is so successful. A lot of web developers, has achieved a number of jsp,php,ruby and other languages, and now there are people familiar with the JavaScript can also do back-end, will naturally be hit stick. node. JS meets the needs of JavaScript for full stack development.
    2. By asynchronously reconciling the events, so that the current process is focused on the calculation, the time-consuming operations such as IO are processed by other threads and then back to the current thread, each page has a reasonable response times compared to PHP (one page does not affect other pages), and he has no complexity compared to Java's multithreaded programming ( No need to lock and consider successively).

Disadvantages:

  1. Too many asynchronous callbacks bring a lot of trouble to writing, are very unfriendly to exception handling, are more likely to cause coupling between modules, and seem to save stack overhead compared to stackfull coroutine, but in fact closures or promise can be more structured, memory, GC and call consumption do not necessarily save.
  2. Version of JavaScript. The current version of JavaScript is not very comfortable to write code (and no other language has so many compile to JS projects like JavaScript), so there are each language compiled into JavaScript, There are also ECMASCRIPT6 and ECMAScript7. So far, the stable version of node. JS (4.2.4) is still not supported by default ECMASCRIPT6, which requires the addition of--use-strict--harmony parameters. Of course I recommend writing this parameter to a script similar to gulp.
  3. Project dependencies and lookups are bad. Development, the official recommendation is to set dependencies through Package.json, NPM automatically installs the dependency package, but NPM inside the package quality is uneven, I encountered several packages in similar socket.on (' Error ') of the callback directly throw exception, There are also some compatibility issues. and the search path of this project and Node_modules writing is completely different, one is./xxx/xxx one is xxx, found that the package has a problem to put in the project, but also to modify the numerous require. In addition one is not very affected the experience of the flaw, node_modules inside seemingly each project has its own dependency package to download to their own sub-node_modules folder inside, if the dependency package is also dependent, and will continue to include, resulting in a very deep folder hierarchy and path, I have to say that this is a very wonderful practice.
  4. node. js is easily misused to think that his session is relevant, trying to cache something in memory, and node. JS does not actually make any prohibitions or warnings about this practice. Unlike CGI, such as PHP, each request is a new page interpretation, there is no global variable (in order to speed up may cache parsing results, but only the cache bytecode or syntax tree, the environment is definitely new), and node. JS requests do not empty the environment. This one doesn't seem to be a problem, and I might be able to use it to do some of the more difficult responses (such as transferring files in real time) to a common server. However, because PM2 and other startup tools, as well as the new cluster launch of node later versions, will cause multiple requests from the same user to be distributed to different processes without your knowledge.

The above is a node. js Beginner's use of feelings, may seem to grumble, but also because the lesson is profound, love and hate intertwined. At least 2 points so far I will never forget:

    1. Do not throw exceptions inside the callback function
    2. Do not attempt to cache processing results in global variables, preferably not even global variables

In other words, the official documentation is so bad that even the parameter descriptions of the callback function are not.

node. JS usage sentiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.