node. JS Learning 02

Source: Internet
Author: User

Path problems in read-write files
    1. ReadFile () reads the./(relative path) parameter in the file function, relative to the path that executes the node command, rather than looking for the JS file that is being executed. To solve this problem:
      __dirname (two underscore): Indicates the directory where the JS file is currently executing
      __filename: Represents the full path of the currently executing JS file
      let filename = __dirname+‘\\‘+‘hello.txt‘
      In the code above: ' \ ' The first \ means the escape character, Hello.txt represents the file to be read

      Path stitching through the path module
    2. Benefits of Path stitching using the path module: No consideration for operating system compatibility
    3. let path = require(‘path‘); let filename = path.join(__dirname,‘hello.txt‘);

      Create a folder from the FS module
    4. For example, create an FS folder with the "Notes" folder under the FS folder

      let fs = require(‘fs‘); fs.mkdir(‘./fs‘,function(err){ if(err){ throw err; } }); fs.mkdir(‘./fs/笔记‘,function(err){ if(err){ throw err; } });

Some problems to be aware of
    1. An asynchronous operation cannot catch an exception through Try-catch, and it needs to be judged by err to determine if there is an error.
    2. Synchronization operations can catch exceptions through Try-catch.
    3. Do not use fs.exists(path,callback) to determine whether the file exists, directly determine the error.
    4. Path issues when working with files
      • When reading and writing files './' means the path of the currently executing node command, not the path of the JS file being executed.
      • __dirname is always "the directory of JS currently being executed"
      • __filename represents "The file name of the JS being executed (including the path)"
Writing an HTTP service program through node. JS, a minimalist version

Steps

    1. Loading HTTP Modules
    2. Creating an HTTP Service
    3. To add a request event handler for an HTTP service object
    4. Enable HTTP Service listener, ready to receive client requests

      `
      HTTP Service Program

      1. Loading the HTTP service
      Let HTTP = require (' http ');

      2. Create an HTTP service object
      Let server = Http.createserver ();

      3. Listening for user request events
      The Request object contains all the contents of the user request message, and the request object can get all the data submitted by the user.
      The response object is used to correspond some data to the user and must use the response object when the server responds to the data to the client
      Request shorthand for REQ response to Res
      Server.on (' request ', function (req,res) {
      Send back a response to the browser
      Res.write (' Hello World ');
      For each request server must end the response, otherwise the browser will assume that the server responds to a no end,
      Res.end ();
      })

      4. Start the service
      Server.listen (8080,function () {
      Console.log (' server started, please visit ' http://localhost:8080 ');
      })`

node. JS Learning 02

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.