In Node, the use of ES programming without the DOM, the BOM (Document, window), Node partial bottom
Terms of Use:
- String single quotes ''
- Keyword, function name spaces
- Use ===
- To err deal
- Except for the prefix plus the window, document and navigator when using global variables browser
- When the line of code is to ([ `the beginning of time, before the three sign a semicolon
when Backticks ES6 in the new template string that supports wrapping and stitching variables
Core Module
FS (File System) core module (file operation function)
fs (file system) core module, provides an API for all file operations
- Require the use of load fs core module
var fs = require('fs')
- Read File
fs.readFile('a.txt ',function(err,data){console.log(data.toString());})
Reference 1: read file path, reference 2: callback function can be used data.toString () - Write file
fs.writeFile('a.txt','nihao',function(error){console.log("写入成功!");})
http core modules (created write server)
- Http kernel module is loaded
var http = require('http')
- Use http.creatServer () to create a Web server , a server instance is returned
var server = http.creatServer()
- Effect server: serving for data retransmission request, receives the request, processing the request, the response request
server.on('request',function(req,res){})
REQUEST request event handler, there are two parameters:. A request request object: to obtain a number of requests client information, such as request path
b . response in response to: sending response information
Address: req.socket.remoteAddress
port number: req.socket.remotePort
IP address used to locate the computer, the port number used to locate a specific application
port number at the same time can only be occupied by a program, but the computer can open multiple simultaneous the service
server is a content default data transmission utf-8 encoded may be encoded with the default operating system is not the same
response.setHeader ( 'inform the data type', 'text / plain general text type, html using text / html; charset = utf-8 ') [HTML page may be a meta]
server.on("request",function(req,res){
console.log("收到客户端请求,请求路径:"+req.url)
console.log("地址:",req.socket.remoteAddress,req.socket.remotePort)
var url = request.url
if(url === '/'){
fs.readFile('./htmla/a.html',function(err,data){
if(err){
response.setHeader('Content-Type','text/plain;charset=utf-8')
response.end('文件读取失败,请稍后重试!')
}else{
response.setHeader('Content-Type','text/html;charset=utf-8')
response.end(data)
}
})
})
- Start the server , bind the port number (network traffic)
server.listen(3000,function(){console.log(3000,running...})
- Close using crtl + C
os core module
- Os kernel module is loaded
var os= require('os')
console.log(os.cpus())
It returns an array of objects, which contains information about each logical CPU core.console.log(os.totalmem())
Returns the total amount of system memory as an integer (in bytes).
path core module
- The core module load path
var path= require('path')
- ` path.extname(‘index.html’)`` // Returns: ‘.html’
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: ‘quux.html’path.dirname('/foo/bar/baz/asdf/quux');
// Returns: ‘/foo/bar/baz/asdf’path.parse('/home/user/dir/file.txt')
// Returns: {root:’/’,dir:’/home/uesr/dir’,base:‘file.txt’,ext:’.txt’,name:‘file’}path.join('/foo','bar','baz/sa','..')
// Returns '/ foo / bar / baz / in'
Other members of Node: in each module in addition require, exports and other modules related API, there are two special members
- __dirname: used to obtain the current file directory module belongs to the absolute path
- __filename: used to obtain the current file absolute path of the
file operation, a relative path is not reliable, the path to the file operation is designed as a path (relative path) with respect to which the command execution node
Solution: relative path into a dynamic absolute path (using __dirname / __ filename)path.join(__dirname,'./a.txt')
Note: the path identified by the path identifier and the module file operation is not the same, the path identity module is relative to the current file module , a path which is not affected node command execution.
Modular
What is modular?
- File scope ( only module scope , there is no global scope)
- Communication rule ( use require to load the module, used to derive the members exports interface object module )
Simple, modular (preferentially loaded from the cache)
There are three modules require load.
- User-written file module, using a relative path, add ./ current directory (not province) ... / upper-level directory
require('./a')
orrequire(''./a.js)
thinking? How to enable communication between the module and the module.
Each file module exports the object provides a default null {}
In b.js file:
var foo = 'a'
exports.foo = 'b'
console.log(exports) // Returns : { foo: 'b' }
In a.js file:
var aExports = require('./b')
console.log(aExports) // Returns : { foo: 'b' }
console.log(aExports.foo) // Returns : b
Thoughts? How to use exports to export multiple members (must be in the subject)?
var foo = 'a'
exports.foo = 'b'
exports.a = 123
exports.b = function(){}
console.log(exports)
If a module needs to export directly to a member, it is to get the function / string, rather than mount a way to use module.exports
In Node, each module has its own internal target module var module = {}
of the module object has a member: exports, the default code of the last sentence there return module.exports
Note: module is an object, exports are objects
var module = {
export:{}
}
var foo = 'a'
function add(x,y) {
return x+y
}
exports = add
exports.foo = 'abc'
exports.a = '123' // Return:{}
Not a single loading function with exports, the function must be used to load module.exports
var foo = 'a'
function add(x,y) {
return x+y
}
module.exports = add
module.exports.foo = 'abc'
module.exports.a = '123 // Return : [Function: add] { foo: 'abc', a: '123' }
var a = aExports(1,2)
console.log(a) // Retuen: 3
About paths
- A relative path of the file operation may be omitted
./data/a.txt
,data/a.txt
,/data/a.txt
relative to the current directory - Loading module, the relative path can not be omitted ./