One, foreword

After several years of development, front-end technology generally entered the deep water area, but Web page development has been difficult to meet the needs of enterprises, Node has gradually become a rigid skill. Micro service to repay the support of the fans the front-end, open Node. Js project, system learning Node. Js knowledge, is also in the way of you, provide systematic learning, in the process of learning any problems you can reply in the background, small make up to pull you into the group of learning together, if there is a help to raise, who remember your focus, and not miss every article on dry.

Ii. The history of Node.js

Ryan Dahl is an experienced C/C++ programmer who writes high-performance Web services in C/C++. After trying ֖ and failing ܾ᠋, he found a few keys to designing high-performance, Web servers: event-driven, non-blocking I/O.

Ryan Dahl’s original goal was to write an event-driven, non-blocking I/O Web server to achieve higher performance and provide an alternative to servers like Apache. But writing in C/C++ is just too painful. So the brother began to envision developing Web services in a high-level language.

He evaluated a number of high-level languages and found that JavaScript had lower development barriers than C and less historical baggage than Lua. Although server-side JavaScript has been around for many years, the back end has been unmarketable and empty, with zero historical baggage. JavaScript is single-threaded, cannot perform synchronous IO operations at all, and there is no additional resistance to importing non-blocking I/O libraries into it. So JavaScript was chosen as the implementation language for Node.js.

At the time, the second browser war was also drawing to a close, with Chrome’s JavaScript engine V8 taking the top spot. And because V8 is an open source JavaScript engine, with a large team investing in and continuing support from Google, it was a natural choice to use V8 as the runtime engine.

So in 2009, Ryan officially launched an open source Web server project named Node.js based on JavaScript and V8 engine. Despite its name, Node was the first to bring JavaScript to back-end server development, and there were already millions of JavaScript developers around the world, so Node.js became very popular.

At first, Ryan Dahl said his project for the web, js, is a web server, but the development of the project over the development of a web server by the his original pure idea, becomes a build a basic framework of networks, so that we can on the basis of it to build more, such as server, client, command line tools.

What is Node.js

Read node.js and read how it came to be. Node.js is not a new language, nor is it a framework for JavaScript. It is a platform where JavaScript runs outside of the browser, server-side JavaScript or Node.js is server-side JavaScript The interpreter is a Javascript runtime. It adopts event-driven, asynchronous programming, designed for network services, and implements functions that Core JavaScript does not have or is not perfect, such as file system, modules, packages, operating system API, network communication, etc. It’s essentially a encapsulation of the Google V8 engine. The V8 engine executes Javascript very fast and performs very well. Node.js optimizes some special use cases and provides alternative apis to make V8 work better in non-browser environments. Written in plain English, is a can compile your js code and let the computer identification and implementation of compiler environment, normally we would use the kernel to compile the resolution to the browser our js code, and the Node. Js is another kind of running environment (so Node. Js is a platform that is not a kind of language, more than what the back-end language, is not a front frame, It is a virtual machine that runs JavaScript written in C++.

4. Application scenarios of Node.js

Node.js can be the cornerstone of a front-end full stack, which can be connected to the front-end and used for back-end applications. Its application scenarios have evolved from scaffolding and helper classes for front-end tools to API intermediates, proxy layers, and professional back-end development.

The most valuable of these is the API middle tier. When the front and back end are working together, sometimes the front end needs to wait for the back end interface because of the schedule difference. In order to ensure the progress, the front-end can use Node as the API middle layer, through nodeJs can agree on their own data format, customize their own interface according to the needs of the front-end, encapsulation of the database to add, delete, change and check operations, create their own proxy and gateway server.

These things used to be left to the back end, but now with nodeJs, the front end can take care of the back-end services themselves, making the process much more manageable.

Sometimes, just using Node.js can be used as an intermediary layer to distribute call data interfaces. For example, if you have a website data provided by Java, you can use Node.js as an intermediary layer to accept user requests, and then use Node.js to call the Java data interface. After retrieving the data, do the HTML composition directly at the Node.js level, and then render the rendered page directly to the user. Node.js is known as a high-performance Web server that is much better at concurrency and stress tolerance than traditional platforms, so such a package can greatly reduce server development.

5. Advantages of Node.js

Node.js is characterized by asynchronous I\O and event-driven

  1. Single thread
  1. Asynchronous non-blocking I/O
  1. event-driven
  1. Parsing JS code
  • There are no browser security level restrictions;
  1. Provide system-level apis;
  • A. Read and write files
  • B. Process management
  • C. Network communication
  1. Node.js libraries are asynchronous and event-driven
  • Node.js uses a single-threaded model, with asynchronous requests for all I/ OS, avoiding frequent context switches. Node.js maintains an event queue during execution, and the program enters the event cycle to wait for the next event. After each asynchronous I/O request is completed, it will be pushed to the event queue for processing by the program process. All API asynchrony is non-blocking. This means that a Node.js-based server will not wait for the API to return data. After the server moves to the next API and calls it, a notification mechanism for node.js events helps the server get the response from the API call. Node.js processes only one event at a time, and immediately enters the event loop to check and process subsequent events. The advantage of this is that the CPU and memory focus on one thing at a time, while keeping time-consuming I/O operations as parallel as possible. For low-speed connection attacks, Node.js only adds requests to the event queue and waits for the response from the operating system, so there is no multithreading overhead, which can greatly improve the robustness of Web applications and prevent malicious attacks.
  1. Very fast
  • Being built on Google Chrome’s V8 JavaScript engine, node.js library code execution is very fast.
  1. Single threaded but highly extensible
  • Node.js uses an event loop single-threaded model. The event mechanism helps the server respond in a non-blocking manner and makes the server highly scalable, rather than the traditional server where it creates thread limits to handle requests. Node.js uses single-threaded programs and the same programs to handle much larger numbers of requests than a traditional server, for example, than an Apache HTTP server.
  1. There is no buffer
  • Node.js applications never buffer any data. These applications simply output the data in the block. (Streaming applications)
  1. license
  • Node.js is distributed under license from MIT.

What can Node do

  • A site with complex logic
  • Large-scale Web applications based on social networks
  • Web Socket website
  • TCP\UDP socket application
  • Command line tool
  • Interactive terminal program
  • Native application with graphical user interface
  • Unit testing tool
  • Client-side Javascript compiler

7. Related concepts

  • npm
  • package.json
  • express
  • koa
  • egg

8. Focus on us

Public number search: front-end micro services