Getting started with Node.js
1. What is Node.js
It is a platform built based on chrome js runtime and based on V8 engine. Simply put, Node.js is JavaScript running on the server.
The difference with JS:
- Based on asynchronous I / O related interface
- Module dependencies based on node_modules and require
- Provide C ++ plugin API to interact with the underlying system
What can Node.js do?
- Web server: Web Server, crawler
- CLI command line script: webpack
- GUI client software: VSCode, NetEase Cloud Music
- loT, image processing, real-time communication, cryptocurrency ...
2. Node.js basics
2.1 Module
- Built-in modules: compiled into Node, such as http fs net process path, etc.
fs = require('fs')
- File module: modules other than native modules, one-to-one correspondence with files (folders)
require('./circle.js')
2.2 Module loading
// 加载绝对路径文件
require('/foo/bar/a.js');
// 加载相对路径文件
require('../a.js');
// 加载无后缀的文件
require('../a');
// 加载外部模块
require('pkg-name'); //从当前或上级或上上级查找直到查到底层根目录
2.3 Module type
- .js
- .json forms js object
- .node binary
- .mjs
- …
2.4 Module path search
- Absolute path
- relative path
- And the current path is treated as an absolute path
- Module / folder
- Native module, read cache directly
- [$NODE_PATH, ~/.node_modules,
./node_modules, …/node_modules, …] - Parse package.json to find the main attribute, if not, use index.js
- If not found, an error is reported
2.5 js module analysis
-
Get the file content synchronously through fs.readFileSync
-
Pack content (closure)
(function (exports, require, module, __filename, __dirname) { var circle = require('./circle.js'); console.log('The area is ' + circle.area(4)); });
-
Execute through vm.runInThisContext
-
Get the value of the module object as the return value of the module
2.6 Module cache
- After the module is loaded, the return value is cached
- Read the cached result directly at the next load to avoid file I / O and parsing time
- The exported object is cached on the Module._cache object
3. NPM
3.1 Package management
- A package.json file should exist in the top-level directory of the package
- Binary files should be included in the bin directory
- JavaScript code should be included in the lib directory
- The document should be in the doc directory
- Unit tests should be in the test directory
3.2 Package dependencies
"dependencies": {
"accepts": "^1.2.2",
"content-disposition": "~0.5.0",
"cookies": "~0.7.0",
"debug": "*",
"delegates": "^1.0.0",
"escape-html": "~1.0.1",
"fresh": "^0.5.2",
"only": "0.0.2",
"parseurl": "^1.3.0",
"statuses": "^1.2.0",
"type-is": "^1.5.5",
"vary": "^1.0.0"
},
3.3 NPM issues
- Speed problem
- safe question
- View source code to see Star
- https://snyk.io/ External inspection service
- asl audit
4. Web development based on Node.js
-
Koa: The role of middleware is not regulated and is not conducive to team development. There are many middleware, uneven quality and difficult selection.
-
ThinkJS: specifies the project structure
├─src
│ ├─bootstrap
│ ├─config
│ │ ├─config.js
│ │ └─adapter.js
│ ├─controller //
routing│
│ ├─index.js
│ ├─logic │ │ ├─index.js
│ └─model // database
├─view // template
file│
├─index_index.html └─www // static resource│
└─static
│ ├─css
│ ├─img
│ └─js
├ ─development.js
├─production.js
├─package.json
TODO List project combat
function list
- TODO List page
- API (RESTful interface specification)
- Get TOO list
- Increase TODO
- Remove TODO
-Update TODO status
RESTful interface specification
- Each API corresponds to a resource or collection of resources
- Use HTTP Method to represent actions on resources
- Use HTTP Status Code to represent resource operation results
Data validation
- Logic mechanism revolving door is provided to support data verification
- File and Action correspond to Controller one by one
Model benefits
- Simplify code and increase efficiency
- Can operate the database without too much understanding of SQL statements
- Avoid the security risks of handwritten SQL statements
5. Debugging Node.js
debugging
Reference: Use ndb debug your project Node.js
- Log debugging
- Breakpoint debugging
- node --inspect
- vscode
- ndb
Node development role change
- front end
- Dealing with browsers, compatibility issues
- Componentization
- Loading speed, JS execution performance, rendering performance
- Error monitoring
- XSS, CSRF and other security vulnerabilities
- Server
- Database, Redis and other peripheral services
- Performance, memory leaks, CPU, machine management
- Service monitoring, error monitoring, flow monitoring, alarm
- SQL injection, directory traversal and other security vulnerabilities