In preparation for the interview, I’ve put together a list of things that Nodejs is often asked about in interviews, and I’ll add them later. If there’s anything wrong, please point it out
Related interview questions:
- Differences between Express and KOA, pros and cons
- Nodejs pros and cons
- Node middle layer details processing
- Event loop mechanism, node and browser event loop mechanism is different
- Node multi-threading, high concurrency, security
Can link part of this article have a detailed explanation, but because the nuggets markdown does not support anchor syntax, do not know how to jump to this page should be the location, so click no response, is the feeling of heartbreak jio
nodejs
define
Node.js is a JavaScript runtime based on the Chrome V8 engine
Nodejs can parse and execute JS code. Previously, only browsers could execute JS code. With NodeJS, JS code can be executed without the browser
The characteristics of nodejs
- event-drivenevent-driven
- non-blocking I/O modelNon-blocking I/O model
- lightweight and efficientLightweight and efficient
Module system in Node
- Modularity is a method of breaking up a project into separate functional parts
- inNodeIn, there is no global scope, only module scope
- Modularized files, with module scope, can not access external variables, internal variables can not access, are closed by default. The namespace of each file is independent and closed, and different modules do not interact with each other and there is no pollution
- It is only through
export
Expose it so the rest of the files can get throughrequire
get
Advantages and disadvantages of modularity
- advantages
- maintainability
- Multi-party cooperation without interference solves the naming conflict that may occur in the process of multi-party development
- Flexible architecture, separation of focus
- Convenient module combination, decomposition
- It is convenient to debug and upgrade a single module
- Separable unit tests
- maintainability
- disadvantages
- Performance loss
- The system is layered, the call chain is long
- Module to module communication, sending messages between modules can be very performance consuming
- Performance loss
commonjs
Nodejs does not support modularity by nature (es6 only supports it), but there is a special modularity support for JS in the NodeJS environment called CommonJS
loadingrequire
var fs = require('fs')
Copy the code
- role
- Load and execute code in modules (synchronization)
- Gets the interface object exported from the loaded module
exportexports
- nodeAll members in the default file are only valid in the current file module, even if loaded, also cannot be accessed
- Only mount to
exports
Can be accessed from the interface object
exports
和 module.exports
The difference between
-
In Node, each module has its own Module object inside it
-
Module. exports is also an object (empty by default)
-
Exports only need to mount the exported member to module.exports
-
Module.exports. XXX = XXX is a cumbersome way to export an interface member each time. Node provides an exports variable to simplify operations. Var exports = module.exports
-
Assigning directly to exports does not work when a module needs to export individual members. Exports assignments disconnect references to module.exports. Similarly, reassigning module. Exports breaks
-
Module. exports is the last module to return, not exports, so reassigning exports has no effect
// exports exposes a class using the module.exports syntax module.exports = constructor name// In general, a js file provides a function for others to use. You just need to expose the function exports.msg = msg Copy the code
Module loading rules
- Loading from cache is preferred
- If you have already
require
Too, do not repeat the loading, can directly get the inside of the interface object - The purpose is to avoid repeated loading and improve module loading efficiency
- If you have already
- Determine the module identifier
Require (' module identifier ')
- The core module
- Custom modules (module identification in path form)
- . /.. / / XXX, d/a/foo js
- The/in the first place is an absolute path, representing the root directory of the disk where the current file module resides c:/
- Third-party modules
The core module
- The core module is made up ofNodeProvides named modules, each with its own special name identification
- For example, file manipulationfsThe module
- httpservice-builthttpThe module
- pathPath operation module
- osOperating system module
- Core modules must be introduced to use
Third-party modules
- All third party modules must passnpmTo download
- You can pass it when you use it
Require (' package name)
Can be used to load - It is not possible for any third party package name to be the same as the core module name
- If it is neither a core module nor a path module, find the directory in which the current file residesnode_modulesdirectory
- node_modulesBelow the corresponding modulepackage.jsonIn the filemainProperty
art-template
The entry module ifpackage.jsonFile does not exist ormainIf the specified entry module does not exist, thennodeIt will automatically find it in that directoryindex.js - The files are actually loaded in the end
- node_modulesBelow the corresponding modulepackage.jsonIn the filemainProperty
- If none of the above conditions is true, it goes to the upper directorynode_modulesDirectory search, if the previous level does not exist, go up to the next level to find…… If the root directory of the current disk cannot be found, an error is reported
- A project has one and only onenode_modules, in the project root directory, so that all the code in the project’s subdirectories can be loaded into third-party packages
Custom modules
- When loading the file, the relative path must add./, otherwise the default is to load the core module
A small extension
require()
The path from the current onejsFile departure, locate the target file- whilefsFind the target file from the command prompt
- fsOther modules use paths with respect tocmdCommand cursor position
- So, inb.jsWant to read1.txtThe recommended absolute path is:
fs.readFile(__dirname + "/1.txt".function(err,data){ if(err) { throw err; } console.log(data.toString()); }) Copy the code
npm
NPM — Node Package Manager: The largest open source library ecosystem in the world, most javascripts related packages are stored on NPM to make it easier for developers to download and use
- npmIs based onnodejsDeveloped package management tools, so usenpmIs installed firstnode
- npmThe second meaning of “is a command line tool
Resolve NPM wall issues
NPM stores package files on servers in foreign countries, which are sometimes walled and slow
- Method 1: Install Taobao mirrorcnpm
Install CNPM install npm install --global cnpm Copy the code
- Method two: Do not want to installcnpmWant to download through taobao server
npm install jquery --registry=http://registry.npm.taobao.org Copy the code
- Method three: every time manual write taobao parameters feel troublesome, and do not want to becnpm, then the configuration is as follows (recommended)
npm config set registry http://registry.npm.taobao.org # Check whether NPM is successfully configured. After successful, NPM install will pass Taobao image by default npm config list Copy the code
Package. The json file
Purpose is to:
- Package description file (current project specification). Create method:
npm init
- Save third-party package dependency information, thannode_modulesClear.package.jsonThe file is equivalent to being used by others when a copy is provided for installation of allAutomatic download index of dependent packages
- dependenciesDependencies that need to be used in a production environment
- devDependencies: Dependencies used in development and test environments
- Allows us to use “semantic versioning rules” to specify the versions of dependent packages in a project
- Make your build easier to share with other developers and easy to reuse
NPM can directly run scripts specified by scripts in package.json
To remove the dependency information when deleting a package, the command is NPM uninstall the package name –save
Package – the lock. The json file
- npm5Not beforepackage-lock.jsonThis file is added later
- npm5Later versions of the installation package do not need to add
--save
Parameter to automatically save dependency information - When a package is installed, it is automatically created or updatedpackage-lock.json.package.jsonDoes not automatically generate
- package-lock.jsonWill savenode_modulesAll package information (version, download address), so re
npm i
The speed can be increased - According to the files,lockTo lock the version, for example
1.1.1
. onlypackage.json, will appear^ 1.1.1
mean1.1.1
Versions above can be used,npm i
The latest version of the package will be downloaded directly, but we want to always download1.1.1
Version, do not download the latest version, sometimes version updates will causeAPIWait for a problem. You can lock the specific version number to prevent automatic upgrade
- npm5Later versions of the installation package do not need to add
Package – lock. Json
- Keep dependency information in mind for faster loading
- To ensure system stability, lock the version number to prevent automatic upgrade
Event loop mechanism
- What is synchronous asynchronism
- synchronous
- Wait for the called to complete execution before continuing
- Blocks the execution of subsequent code
- asynchronous
- There is no need to wait for the caller to respond, active polling by the caller and active notification by the caller
- Does not block the execution of subsequent code
- Difference: active waiting or passive notification, blocking or not
- synchronous
- What is blocking non-blocking
- Difference: call status, whether the caller waits or does not delay in the process of obtaining the result
- Asynchronous non-blocking saves the caller time (nodejsOne feature)
- What is theAsynchronous I/o
- I/O capabilities provided by the operating system
- Visible IO capabilities in daily life: human-computer interaction, data in and out, physical interface such as mouse and keyboard as input, display as output. These interfaces go down to the operating system layer. The operating system provides various capabilities (disk read and write, DNS query, and database connection) plus the interaction between upper-layer applications and lower-layer systems. Synchronous BLOCKING is synchronous I/O, and asynchronous non-blocking is asynchronous I/O
- nodejs 中 fsIn the modulereadFileFile reading and writing is typicalAsynchronous I/o.readFileSyncisSynchronous IO
- What is theSingle thread
- You can only do one thing at a time. Two pieces of JS code cannot be executed at the same time
- The reason is to avoid DOM rendering conflicts. When multiple lines of JS are executed at the same time, DOM manipulation may occur at the same time, causing conflicts
- Asynchronous js is a single thread solution, is a helpless solution, there are many problems
- Not executed as written, poor readability
- callbackIt is not easy to modularize
- implementationevent-loop
The browser event loop mechanismevent-loop
- Js realization of asynchronous specific solutions
- Synchronized code, executed directly in the main process
- Asynchronous functions are first placed in asynchronous queues
- After the synchronization function is complete, polling performs tasks in the asynchronous queue
- Macro taskmacrotask
- Script overall code
- setTimeout
- setInterval
- setImmediate
- I/O
- ui render
- Micro tasksmicrotask
- process.nextTick
- promise.then
- Async /await (promise)
- MutationObserver (new h5 feature)
- Macro taskmacrotask
- The JS asynchronous mechanism consists of event loops and task queues. JS itself is a single threaded language, the so-called asynchronous depends on the browser or operating system to complete. The JavaScript main thread has an execution stack and a task queue, and the main thread executes the code in turn
- An asynchronous operation is encountered (e.g. When setTimeout (AJAX), the asynchronous operation will be performed by the browser (OS). After the completion of these tasks, the browser will push the pre-defined callback function to the task queue of the main thread. When the execution stack of the main thread is cleared, the callback function in the task queue will be read. After the task queue is read, the main thread continues to execute in an infinite loop called an event loop
- Polling monitoring asynchronous queue, the current time there is a function, get the main process to execute, after the execution and monitoring asynchronous queue, there is a function to the main process to execute… So the polling
- After each macro task, all the microtasks in the play task queue are executed, followed by the next macro task, and so on
Nodejs event loop mechanism
- In Node, the event loop model is roughly the same as in the browser, but the major difference is that Node has different stages of the event loop
- The main difference between the two is that the microtasks in the browser are performed in each corresponding macro task, whereas the microtasks in NodeJS are performed in different stages
- The uv_run function in Libuv implements the entire event loop (6 stages)
- timer
- setTimeout
- setInterval
- IO callback Event callback phase
- eventEmitter
- Idle, prepare
- For internal use only
- Poll Polling phase
- Retrieves new I/O events, performs I/ O-related callbacks (in almost all cases, except for closed callback functions, those scheduled by timers and setImmediate()), where Node will block at the appropriate time.
- Incoming data Indicates incoming data
- fs.readFile
- Retrieves new I/O events, performs I/ O-related callbacks (in almost all cases, except for closed callback functions, those scheduled by timers and setImmediate()), where Node will block at the appropriate time.
- Check phase
- Do setImmediate directly
- Close callback Closes the callback of the event
- If a socket or handle is suddenly shut down (such as socket.destroy()), a close event is emitted at this stage, otherwise it is emitted via process.nexttick ()
- timer
- Most asynchronous tasks in daily development are processed in the poll, Check, and timers stages. The poll stage is briefly described
- If a timer already exists and a timer is running out, the eventLoop returns to the Timers phase
- If there is no timer, it will look at the callback function queue
- If the poll queue is not empty, the callback queue is traversed and executed synchronously until the queue is empty or the system limit is reached
- If the poll queue is empty, two things happen
- If there is a setImmediate callback to perform, the poll phase stops and enters the Check phase to perform the callback
- If no setImmediate callback needs to be executed, the state waits for the callback to be added to the queue and then executes it immediately. The state also has a timeout setting that prevents the state from waiting and then entering the Check phase
- SetTimeout and setInterval
- If both are called in the main module, the order of execution depends on process performance, i.e., randomness
- If neither is called by the main module (wrapped by an asynchronous operation), the setImmediate callback is always executed first
Pros and cons of NodeJS
(Good at I/O intensive, bad at computing intensive….)
- advantages
- Higher performance in high concurrency scenarios
- Suitable for I/O intensive applications
- disadvantages
- It is not CPU intensive
- Applications with heavy CPU usage and low I/O usage, such as video coding and artificial intelligence, cannot take advantage of Node.js
- Nodejs is single-threaded and supports only single-core cpus, which cannot take full advantage of the performance of multi-core cpus
- Reliability is low, once a part of the code crashes, the whole system crashes
- It is not CPU intensive
- The solution
- Nnigx reverse proxy, load balancing, open multiple processes, bind multiple ports
- Single thread to multi-thread: open multiple processes listening on the same port, using the Cluster module
Nodejs is suitable for handling high concurrency causes
- The single thread solution to high concurrency is to use non-blocking, asynchronous programming ideas. A simple summary is that when the IO operation is very time-consuming, it uses a non-blocking way, continues to execute the following code, and enters the event loop. When the IO operation is complete, the program will be notified that the IO operation is complete. The main use of JavaScript callback function to achieve.
- Although multithreading can also solve high concurrency, it is realized by establishing multiple threads. Its disadvantage is that when it encounters time-consuming IO operations, the current thread will be blocked, and the control of CPU will be handed over to other threads, which brings the problem of very frequent context switching of threads
1. The asynchronous I/O
Node has the asynchronous I/O feature. Each time an I/O request occurs, Node provides an I/O thread for that request. Node then ignores the I/O process and continues to execute events on the main thread only when the request returns for a callback. Node saves a lot of time waiting for requests
2. Event driven
The main thread runs the program by firing an event loop
This applies to nodeJS scenarios
- RESTful API
- Unify the UI layer of Web applications
- Application of a large number of Ajax requests
- Develop command-line tools
node
andapache
Such as the difference between server software
- It doesn’t have its own syntax, uses V8 engine, so it’s JS. Node simply ports V8 functionality to the server without rewriting it. The V8 engine parses JS very efficiently, and a lot of things in V8 are asynchronous.
- Node.js has no concept of a root directory, because it doesn’t have any Web containers at all, except for a root directory once installed and configured. Have Node.js provide a static service that needs to be wrapped itself
Node serves as the middle tier
Why introduce the Node middle tier
Speaking of which, it’s important to understand the pitfalls of client-side rendering when the front and back ends are separated
Server side rendering
- Basically, using the template engine on the server, the data request is rendered onto the template before it is sent to the client
Var render = template.render('Template string', {parse replacement object})Copy the code
- It’s the MVC pattern
- In this mode, the pressure on the server is very heavy and the service on the server is very complicated. Therefore, the front and back ends need to be separated to ensure that the back end only uses the response data
Client-side rendering
- The first request to get the page
- Second request to get the data
- Get the Ajax response on the client side and render the data
- The MVVM pattern
How to determine server side and client side rendering
- Look at the site source code, if the dynamic request data can be searched, it is server rendering
- If the page is not refreshed, but the page is switched, it must be client rendering
Client-side rendering defects
- The first screen is white for a long time
- Server side rendering can be captured by crawlers, but client side rendering can not be captured by crawlers, which is not conducive to SEO search engine optimization
- When the template engine parses, it is all done by the browser, which is overloaded
- Client rendering sometimes has cross-domain interface issues
To solve many of the problems of client rendering, node can be introduced as an intermediate layer. Using nodeJS as an intermediate layer to render data makes up for SEO friendliness that front-end template engines and routing can’t do
Node is handled in detail as the middle tier
The whole flow chart is as follows:
- After the client sends the request, it receives a complete HTML page, which is conducive to search engine optimization
- Node middle layer can complete server-side rendering, but different from traditional SSR
- Node’s high concurrency attributes make it more suitable for large projects and improve rendering efficiency
- Node can use Redis to cache back-end data
- Node can perform request merging and load balancing to share the high concurrency burden at the back end
Use the template engine in Node
- art-templateYou can use both the front and back ends
- ejsIt is a string, which is inefficient
- jadeHigh efficiency, but high cost of learning
express
The middleware
What is middleware
- The nature of processing requests is functions
- The user processes the processing environment from request to response step by step, and each step is completed by calling a method, which is middleware (intermediate processing link).
- Processing and packaging (such as query, postBody, cookies, session), returns a method, mounted on the req
- It must be mounted before the route because it needs to be used in the route, receiving three parameters (req, RES,next)
Middleware in Express
There are several categories of middleware in Express, with all middleware operating on the same REQ, RES in the same request
1. Application-level middleware
Universal matching, middleware that doesn’t care about request paths and request methods
- Any request will go to this middleware
var express = require('express') var app = express() // Type 1: app.use app.use(function(req, res, next){ console.log('Request is coming in') // Not calling next() will not enter the second middleware next() }) // Configure 404 middleware app.use(function(req, res){ console.log('Second middleware, 404 now') res.render('404. html') }) app.listen(8888.function() { console.log('running')})Copy the code
Middleware that cares about the request path
-
Any path that starts with ‘/a’ can be entered, regardless of the subpath
-
‘/ a/b
-
‘/ ab
app.use('/a'.function(req, res, next){ / / http://localhost:8888/a/b in / / http://localhost:8888/a in / / http://localhost:8888/ab / / http://localhost:8888 console.log('/a path request is coming in ')})Copy the code
2. Routing level middleware
- Middleware that strictly matches request paths and methods
- Strictly matches, ‘/a’ matches
- ‘/ a/b
app.get('/a'.function(req, res, next){ / / http://localhost:8888/a/b / / http://localhost:8888/a in / / http://localhost:8888/ab / / http://localhost:8888 console.log(1) }) app.post('/'.function(req, res, next){ console.log(2) }) app.pur('/'.function(req, res, next){ console.log(3) }) app.delete('/user'.function(req, res, next){ console.log(4)})Copy the code
3. Error handling middleware
- Global error handling, usually placed after 404 handling
app.use(function(err, req, res, next){ console.error(err.stack) res.status(500).send('error')})Copy the code
4. Built-in middleware
- express.static
- express.json
- express.urlencoded
5. Third-party middleware
- body-parser
- compression
- cookie-parser
- morgan
- response-time
- serve-static
- session
Configuring middleware
- Middleware can be used only after it is mounted
app.use(session({ secret: 'coco', resave: false, saveUninitialized: false }) app.use(router) Copy the code
Koa2, KOA1 and Express
-
Koa1: Relies on the CO library and uses generator functions, using yield statements inside functions
-
Koa2: Added arrow functions, removed CO dependencies, uses Promise, so can be used with async await, ES6 syntax, execution time is faster than KOA1
-
Koa and Express
- Express is big and complete, koA is small and fine
- Koa is a bare framework that doesn’t bind to any middleware. It adds what you need, and it has great scalability
- API contrast
- Koa template engine and routing are not as rich as express apis. Koa mounts both REQ and RES to CTX, which allows access to both REQ and RES
- Although KOA has much less integration than Express, it is more flexible by requiring only the require middleware
- Middleware loading and execution mechanisms
- At the heart of the middleware pattern difference is the implementation of next
- Koa requests and responses are onion in and out of the model, using the latest async code, no callback functions, code runs very cleanly. When koA processing middleware meets await next(), it will suspend the current middleware to process the next middleware, and finally go back to continue processing the remaining tasks (logic is callback function). Recursion has the problem of stack overflow, which may block js engine. Koa adopts the way of tail call to optimize performance
- Express is linear, only in, not out. Express itself does not support the data in and out of the Onion model, so other plug-ins need to be introduced
- App. use is simply stuffing new middleware into the middleware array
- The execution of the Express middleware relies on the private method app.handle
- Koa uses the compose() method to convert and cascade the array of middleware we passed in, and callback() returns the result of this.handlerequest ().
- Programming experience
- Express is a callback function
- Koa2 is based on a new syntactic feature async Function that implements promise chain delivery and is more error-friendly
- Advantages and disadvantages
- express
- Advantages: longer history, more complete documentation, more information, deeply popular
- Cons: Callback hell, no default error handling
- koa
- Advantages: No callback, default error handling
- Disadvantages: Routing, templates, JSONP middleware all need to be configured by the developer (but more flexible)
- express
- Express is big and complete, koA is small and fine