One, the operation of CNMP;
1,CNMP Info jquery Query the version of jquery;
2,CNMP install [email protected];
3,CNMP list to query all downloaded content;
4,CNMP List | grep jquery; show only downloaded juery;
Second, in a certain JS file to enable the building server;
1, using ES5 to create the server;
var http=require ("http");
Http.createserver (function (req,res) {
Res.writehead (200,{"Content-type": "Text/html"});
Res.end ("Hello, world!") ");
}). Listen (8888);
Console.log ("Server running at http://127.0.0.2:8888/");
2, using ES6 to create the server;
Const Http=require ("http");
Const homename= "127.0.0.1";
Const port=3000;
Const Server=http.createserver (function (req,res) {
res.statuscode=200;
Res.setheader ("Content-type": "Text/plain");
Res.end ("Hello! ");
});
Server.listen (Port,hostname, () =>{
Console.log ("Server created successfully! ");
});
Third, the use of commonjs;
1,
First, the HTTP module is introduced;
var http=require ("http");
2,
Then introduce the modules we need;
var util=require ("./util.js");//The file in the same directory to use./;
3,
Http.createserver (function (req,res) {
Res.writehead (200,{"Content-type": "Text/plain"; utf-8});
Util.say (res);//Must be the module introduced;
Util.add (res);//Must be the module introduced;
Res.end ();//res begins, and res ends;
}). Listen (8000);
Console.log ("Modified successfully! ");
Example!!!!
var http=require ("http");
var util=require ("./util.js");
Http.createserver (function (req,res) {
Res.writehead (200,{"Content-type": "Text/plain;charset=utf-8"});
Util.say (RES);
Util.add (RES);
Res.end ();
}). Listen (8000);
Console.log ("Hello! ");
var util={
Say:function (res) {
Res.write ("Nihao");
},
Add:function (res) {
Res.write (1+2+ "");
}
}
Module.exports=util;
A. Define the interface; If you use Var util={} When you define it, remember that it is the JSON format;
B. exposed interface; module.exports=util;
Iv. Use of NPM
1, global installation module; NPM Install N-g
2, project dependent, through--save add in
3, development dependence, the line does not need to---through--dev add in
4, view version ID: CNPM outdated
Five, protocol
Https://www.baidu.com:8080/api.php?from=1000phone&course=nodejs#test
{
Protocol: ' https: ',
Slashes:true,
Auth:null,
Host: ' www.baidu.com:8080 ',
Port: ' 8080 ',
Hostname: ' www.baidu.com ',
Hash: ' #test ',
Search: '? From=1000phone&course=nodejs ',
Query: {from: ' 1000phone ', Course: ' Nodejs '},
Pathname: '/api.php ',
Path: '/api.php?from=1000phone&course=nodejs ',
HREF: ' Https://www.baidu.com:8080/api.php?from=1000phone&course=nodejs#test '}
Six, event monitoring
var eventemitter=require ("Events");
Methods for defining an object in Es6: Class Player extends Eventemitter;
Instantiate a Player object: var player=new player ();
Listening process: Two
1, receive event---> can only have one receive event; Player.on ("Playing", (data) =>{
Console.log (' Playing: ${data} ');
});
2, the distribution of events----> can be distributed in multiple districts; Player.emit ("Playing", "Brief Encounter");
Playing ("playing", "sentiment in");
Seven, document management;
A total of 7 methods:
1, view file status, ()
var fs=require ("FS");
Fs.stat ("File name to view", (err,stats) =>{
if (err) {
Console.log (ERR)
}else{
Console.log (' Directory: ${stat.isdirectory ()} ');
Console.log (' File: ${stat.isfile ()} ');
}
})
2, create directory: Use Fs.mkdir;
3, write the file, and write the content, including a connection;
A: Start without files, create files and write content; fs.writefile;
Fs.writefile ("File path and name created", "Write File Contents", () =>{})
B: Start with file, write content only;
Fs.appendfile ("Write File path and name", "Write File Contents", () =>{})
4. Read file: Take advantage of Fs.readfile ("path and name of file", "Utf-8", () =>{});
If you do not write the second, you will see data returned as buffer type, but there will be an error if there is no such file;
5, read folder; Fs.readdir ("File name", (=>{});
6, file Rename; Fs.rename ("Original file name", "changed file name" () =>{});
7. Delete files (including two steps)
A: Delete the contents of the file;
Fs.readdirsync ("Deleted folder"). Map (item) =>{
Fs.unlink (' Deleted folder/${item} ', (ERR) =>{
if (err) {
Console.log (ERR)
}else{
Console.log ("Success! ");
}
})
});
B: Delete a folder
Fs.rmdir ("Files to be deleted", (Err,data) =>{
if () {}else{};
})
eight, stream;
Since the 1,--save project
2,--save-dev since the development of the project;
3.pipe the output of the previous layer as the input of the next layer;
4.zlib compression module;
Zlib.creategzip ();
node. JS Learning Essays