Hello node. js to build a Web server

Source: Internet
Author: User
Tags node server

node. js Brief

Nodejs is a JavaScript runtime, the interpreter is written in C + +, based on the ChromeV8 engine, the event-driven, non-blocking I/O model. This series currently refers to the node. JS website, the Web Scott's node. JS Foundation, the "node is learned to use" Turing series, as well as possible online open resources.

The Nodejs Package Manager is a NPM package that is written by someone else.

Nodejs provides built-in objects such as fs,http to manipulate disk files and build servers.

Features: single-threaded, event-driven, asynchronous non-blocking I/O models.

node. JS Installation

The official website installs node. js and comes with NPM.

Install Git on your website.

Open a git bash or command-line tool, and the input node-v and NPM-V versions show that the installation is successful.

Build a Web server

Open node. JS Official website https://nodejs.org/en/, in the About column there is a piece of code, copied to our written server.js file. The code is as follows:

1Const HTTP = require (' http ');//Loading HTTP Modules2 3const hostname = ' 127.0.0.1 ';//Host Address4Const PORT = 1337;//Port number5 6Http.createserver ((req, res) = = {//Create Web server 200 Success7Res.writehead, {' Content-type ': ' Text/plain ' });8Res.end (' Hello node.js\n ');9}). Listen (port, hostname, () = {TenConsole.log (' Server running at http://${hostname}:${port}/'); One});

The code to build the Web server is already written, and the Web server is open below:

command-line tool, to the current folder, enter node Server.js, the visible server is turned on.

Now you can go to the browser to visit! Open Browser input 127.0.0.1, page display:

At this point, a Web server can handle the request rendering is complete!

If you save the file after modifying the 8th line of code output, the Refresh browser will not see the change. Because this differs from the browser script file, the server needs to be restarted.

In the command line tool CTRL + C can shut down the Web server, and then restart, then refresh the browser can see the changes.

Hello node. js to build a Web server

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.