Nodejs Learning (1)

Source: Internet
Author: User

1. Install: You can download all versions of Nodejs in http://nodejs.org/dist/, I downloaded the node-v8.9.0-x64.msi in it, the installation is complete, at the command prompt, enter "node--version" to detect whether the installation is successful.

2.node.js Create the first app:

Create a new file named Server.js in the D drive with the following code:

var http = require (' http '); Http.createserver (function (request, response) {    //Send HTTP Header     //HTTP status value: 200:ok
   //content Type: Text/plain    response.writehead ($, {' Content-type ': ' Text/plain '});    Send the response data "Hello World"    response.end (' Hello world\n ');}). Listen (8888);//terminal print the following information Console.log (' Server running at http://127.0.0.1:8888/')

Enter "Node Server.js" to execute the above code with the following results:

3.NPM:NPM is a package management tool that is installed with Nodejs and tests whether NPM was successfully installed by entering "npm-v".

4. Install the module using the NPM command:

The above error occurs because the directory is not installed, modified, the following is a global installation, so you need to configure environment variables:

5.nodejs callback function:

Create the Main.js file with the following code:

var fs = require ("FS"); var data = Fs.readfilesync (' input.txt '); Console.log (data.tostring ()); Console.log ("program execution ends!");

Create the TXT file and Main.js file to ensure that the encoding method is UTF-8, in terms of TXT file, save to pay attention to its encoding.

Nodejs Learning (1)

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.