In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the role of Node.js is for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Welcome to the Node.js world
1.DIRT (data-intensive real-time) stands for data-intensive real-time programs. Because Node itself is very lightweight on Ibind O, it is good at mixing or proxying data from one pipe to another, which holds a lot of open links when processing a large number of requests and takes up only a small portion of memory. Its design goal is to ensure responsiveness.
Second, build a chat room program with multiple rooms
1. Event emitter: it is associated with a resource that can send messages to and receive messages from that resource. Resources can connect to remote servers, or something more abstract, such as a character in a game
III. Basic Node programming
Organization and reuse of A.Node function
The 1.Node module allows you to select functions and variables from the imported file to expose to the program. If the module returns more than one function or variable, it can indicate them by setting the properties of the exports object. However, if the module returns only one function or variable, you can set the module.exports property
2.require takes the path of the module you want to use as a parameter, looks for it in a synchronous way, locates to the module and loads the contents of the file. It is one of the few synchronous Imax O operations in Node. Do not use it in places where it is dense.
3.module.exports can provide a single variable, function, or object. If you create a module with both exports and module.exports, it will return module.exports, while exports will be ignored.
b. Asynchronous programming technology
1. Two ways of response editing management: callback and event monitoring
Callbacks are usually used to define the logic of an one-time response, which is passed as an argument to the asynchronous function, describing what to do after the asynchronous operation is completed.
An event listener is essentially a callback, except that it is associated with a conceptual entity (event).
two。 An Node HTTP server instance is an event emitter, a class that can inherit and add event emission and processing capabilities (EventEmitter).
3. Return from the function as soon as possible during the callback. Most built-in modules in Node take two parameters when using callbacks: one is to put errors that may occur, and the other is to put the results
4. The event emitter triggers events and can handle those events when they are triggered.
5. When creating an asynchronous program, you must pay close attention to the execution process of the program: conditions for event polling, program variables, and other resources that change as the program logic executes.
c. Serialization of Asynchronous Logic
1. The concept of sequential execution of a set of asynchronous tasks is called process control. Divided into:
Serial: need to do one task after another
Parallelism: there is no need to do one task after another
Fourth, build Node Web program
Basic knowledge of A.HTTP server
1. Before ending the response, you can run any asynchronous logic you want to run during the lifetime of the request. If you fail to end the response, the request is suspended until the client times out or remains open
b. Build a RESTful Web service
1.Buffer.byteLength (), byte length, Content-Length should be byte length
c. Provide static file service
1.*.pipe () pipeline
Storing data in Node programs
a. Serverless data storage
1. Memory storage: store a small amount of frequently used data
two。 File-based storage: using the file system to store data, there is a concurrency problem
VI. Connect
There is a concept of mounting in 1.Connect, which can define a path prefix for middleware or the whole program.
two。 When building middleware, you should focus on the small, configurable parts. Build a large number of small, modular, reusable middleware components to build your program. Keeping middleware miniaturized and focused helps to break down complex program logic into smaller components
7. Middleware that comes with Connect
1. Middleware for parsing cookie, request body, and query string: cookieParser () bodyParser (), limit (), query ()
two。 Middleware to realize the core functions of Web program: logger (), favicon (), methodOverride (), host (), session ()
3. Session objects are serialized into JSON objects between requests: circular properties are not allowed, function objects cannot be used, Date objects cannot be serialized correctly, etc.
4. Middleware dealing with Web program security: basicAuth (), csrf (), errorHandler ()
5. Middleware that provides static file services: static (), compress (), directory ()
* most of them are no longer self-contained. Refer to:
Https://github.com/senchalabs/connect
Https://github.com/expressjs/basic-auth-connect
VIII. Express
The _ _ dirname in 1.Node is used to determine the directory where the currently running file is located
2.*express global command installation: sudo npm install-g express-generator (after 4.x)
IX. Express Advanced
1.Node is single-threaded and has no thread local storage. Request and response variables are the only context objects for the HTTP server
Test the Node program
1. Unit test:
TDD:Mocha, nodeunit, assert module
BDD:Mocha 、 Vows 、 should.js
two。 Acceptance testing, also known as functional testing, tests program output rather than logic, and provides another layer of protection to identify bug that may have been missed by unit tests, including Tobi and Soda
11. Web program template
1.ejs: https://github.com/tj/ejs ,nom install ejs
2.Hogan
3.jade
Deploy Node programs and maintain uptime
1. Keep Node running, the Forever tool:
Sudo node install-g forever
Forever start/stop server.js
2.Upstart tools, restart crash programs, log, etc.
Sudo apt-get install upstart
Sudo yum install upstart
Sudo service xxxxx.conf (upstart profile)
3.Node adds cluster API. With this API program, you can run multiple "workers" at the same time at different prices, making it easier for a single program to use multiple cores.
XIII. Surpass Web server
1. Because of its asynchronous nature, Node is suitable for performing intensive tasks that are difficult or inefficient in synchronous environments.
2.Socket.IO allows you to write real-time Web programs using a two-way communication channel between the server and the client. Very popular in browser-based games, chat programs and streaming media applications
3.Buffer is a special data type prepared by Node for developers. It is like a fixed-length slab of raw binary data. Buffers can be treated as arrays, but their size is fixed, and they can only store numbers from 0 to 255 and can handle raw bytes, so they can be used to implement any underlying protocol.
4. A global process object that contains information about the current process, such as the parameters passed to it and the currently set environment variables. Each Node process has a singleton global process object that is shared by all modules.
5.fs module, including high-level ReadStream and WriteStream, etc. Most of the Node documents point to the C function interpretation in the corresponding man page (extension module: fstream module, filed module)
6. To multiply external processes:
Cp.exec (), a high-level API that spawns commands and buffers results in callbacks
Cp.spawn (), which propagates singleton commands into the underlying API in the Child-Process object
Cp.fork (), a special way to spawn additional Node processes using the built-in IPC channel
7. Parsing command line arguments: process.argv
This is the end of this article on "what is the role of Node.js". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.