HTTP Service Let Server = Http.createserver (function (req,res) {}); Monitoring: Server.listen (8080); Request requests response the appropriate editor to configure the appropriate environment sublime: Open Tools---Build system--New build system Chinese: tool--compile System--New compilation system Enter the following code, save the configuration file, and then use the shortcut key Ctrl + B to turn on real-time node monitoring
{ "cmd": ["node","$file"], "File_regex":"^[]*file \ "(... *?) \ ", line ([0-9]*)", "Working_dir":"${project_path:${folder}}", "selector":"Source.js", "Shell":true, "encoding":"Utf-8", "Windows": { "cmd": ["node","$file"] }}
Atom Editor download corresponding plugin script Ctrl + SHIFT + B real-time Monitoring node command res.write (); Output res.end () to foreground; URL to end output Req.url request URL + favicon.ico Facicon.icon is Chrome's own request <link rel= "shortcut icon" HRE F= "" >
ConstHTTP = require ('http'); let server=Http.createserver (function (request, response) {Console.log (Req.url); Switch(req.url) { Case '1.html': Res.write ('1111'); Break; Case '2.html': Res.write ('2222'); Break; default: Res.write ('404'); Break; }; Console.log ('was requested.'); //res.write ();res.end ();}); Server.listen (9090);
http--module file operation: FS module (System comes with module) files System client----Server----disk----Server----client asynchronous vs Synchronous Asynchronous: Multiple operations can be performed simultaneously, each time the previous operation is completed, and the next operation can Start Async: One operation at a time read the file://fs.readfile (file name, callback function (err, data) {}); Fs.readfile ();Data Read Results<buffer Raw binary Data >
const FS = require ('fs'); Fs.readfile ('1.txt') , function (err, data) { if(err) { console.log (' read failed '); } Else { console.log (data.tostring ());} });
Write File//fs.writefile (filename, content, callback function (ERR) {}); Fs.writefile (' 1.txt ', ' Jason is a hero! ', function () {});
const FS = require ('fs'); Fs.writefile ('1.txt') 'Jason is a hero! ' , function (err) { console.log (err);});
Summary: Note that the callback function is an asynchronous function, the Res.end () of the comment, the position is not correct, the FS file read ends before parsing the reason and ending the corresponding, and should not be preceded by the callback to execute the corresponding FS callback
//introducing the HTTP moduleConstHTTP = require ('http');//Introducing the FS moduleConstFS = require ('FS');//Create a connectionLet Server = Http.createserver ((req, res) = = { //Specifying static ResourcesLet file_name ='./www'+Req.url; Fs.readfile (file_name, (err, data)= { if(Err) {Res.write ('404'); } Else{res.write (data); } res.end (); }); //res.end ();});//Listening PortServer.listen (9888);
Nodejs Learning Note One (sublime, Atom development environment, HTTP module, FS module, initial knowledge)