node. JS Basic Learning (Act III)

Source: Internet
Author: User

FS Module operation file:
Fs.readfile () & Fs.readfilesync ()//Read file contents
Fs.writefile () & Fs.writefilesync ()//write content to file
Fs.appendfile () & Fs.appendfilesync ()//append content to File
Fs.unlink () & Fs.unlinksync ()//delete files
Fs.rename () & Fs.renamesync ()//renaming files
When you write a file using WriteFile (), the target file is created automatically if it does not exist, or if it exists, the contents of the file are replaced.
When appending data using AppendFile (), if the destination file does not exist, it is created automatically, and if present, appends the contents to the file.

    

    

--------------------------------------------------------------------------------------------------------------- --------------
HTTP Module:
Used to create client applications or server-side applications that use the HTTP protocol.
Creating and initiating a request message, waiting for and parsing the response message-Implementing the Web Client
Receives and parses the request message, builds and sends a response message-implements the Web server
Common class:
http. Agent
http. Clientrequest
http. Server
http. Serverresponse
http. Incomingmessage
Common methods:
Http.get ()
Http.request ()
Http.createserver ()
--------------------------------------------------------------------------------------------------------------- --------------
Two tools:
1. Http.request: is an HTTP client tool that initiates an HTTP request to the Web server and obtains the response data
There are two main methods: Http.get () http.request ()
The method returns an HTTP. A Clientrequest object that describes the request information, and the parameter of the callback function is an HTTP. A Incomingmessage object that encapsulates the response information.
Common methods for Clientrequest objects:
Write (chunk)//Append request principal data to the server
End ()//Submit Request message body ends
SetTimeout ()//Set Request message time-out time
Abort ()//Termination request
Common events for Clientrequest objects:
Response//Receiving Response message
Abort//Request Termination Event
Error//request has occurred

    
The Http.get () method does not require the method to call end () manually.
--------------------------------------------------------------------------------------------------------------- --------------
2, Http.server: is an event-based HTTP server
Function: Used to create a Web server, receive a client request, and return a response message. All requests are encapsulated in a separate event, and all functions of the HTTP server can be implemented only by writing handlers for the responding events.
Http.createserver ()//create a Web server; The return value of the method is an HTTP. Server object

--------------------------------
http. Common methods for server objects:
Listen ()//start the server and listen for the specified port
Close ()//Stop the server from running
SetTimeout ()//Set timeout time for server response messages

--------------------------------
http. Common events for server objects:
Connection//When a client connection occurs
Request//Received message
Close//Server stop Event
Error//Response process has occurred

There are two parameters in the callback function of the request event:
The first parameter, is an HTTP. A Incomingmessage object that encapsulates the request message information submitted by the client.
The second parameter, is an HTTP. The Serverresponse object that is used to build the response message data that is output to the client.

    

    
--------------------------------------------------------------------------------------------------------------- --------------
MySQL module:
Third-party modules that provide relevant operations for MySQL servers
CreateConnection ()//Create a connection to a MySQL server, return a Connection object
This connection object has the following methods:
Connect ()//Connection database
Query ()//Submit SQL statement to MySQL server for execution
End ()//disconnect from MySQL server

node. JS Basic Learning (Act III)

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.