Use node. js to build an HTTP server locally and node. js to build a server

Source: Internet
Author: User

Use node. js to build an HTTP server locally and node. js to build a server

Our goal is simple. We use node. js to build an HTTP server locally and implement hello word.

System Environment:

Win7 64 bitIP: 127.0.0.1Node.js: v6.10.2Npm: 3.10.10Git: 2.12.2.2-64-bit

Basic installation:

1. Node. js installation packageAnd source code: https://nodejs.org/en/download/

Git: https://git-scm.com/download/win.

2. Follow the installation instructions,Select the installation path and npm.

3. RingDefault environment variableC: \ Users \ Administrator \ AppData \ Roaming \ npm \ node_modules.

Create a new variable

Creation method:My computer> Properties> advanced system Settings> environment variables, create NODE_PATH, and set the value to the current installation directory of nodejs.

The command line tool runs under the current computer user. Therefore, it always starts with C:/Users/XXX/Desktop> and does not relate to the location where nodejs is installed.

After installing node, there are two modes in cmd: Development Mode and command line mode. To enter development mode, enter node and press Enter. At this time, you can enter the code, for example, if require ("express") is entered in command line mode, an error is returned, indicating that 'require 'is not an internal or external command, it is not a running program or batch processing file. Exit the available. exit command from the Development Mode. You can also use quick jet, ctrl + D or two ctrl + C.

4. Create an application

Create a project directory, which can be defined by yourself. In this case, the directory is e:/node/serve.

Since we are building a server, I name the first file server. js.

Enter the following code in server. js:

Const http = require ('http'); // instantiate "http" const hostname = '2017. 0.0.1 '; const port = 3000; const server = http. createServer (req, res) => {res. statusCode = 200; // status value: 200: OKres. setHeader ('content-type', 'text/plain '); // Content Type: text/plainres. end ('Hello World \ n'); // response result "Hello world"}); server. listen (port, hostname, () =>{// listener console. log ('server running at http: // $ {hostname }:: {port }/');});

5. Application Execution

Open the git command line and enter cd e:/node/serve/

Enter node service. js

Open your browser and visit http: // 127.0.0.1: 3000/. You will see a webpage with the words "Hello World ".

I just got started with NodeJs and haven't learned more deeply yet. My understanding of its operating mechanism is not necessarily correct. You are welcome to criticize and correct it.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.