Nodejs Learning Note One (sublime, Atom development environment, HTTP module, FS module, initial knowledge)

Source: Internet
Author: User
Tags readfile

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.