Node. js [2] Hello Node

Source: Internet
Author: User
Document directory
  • Node Installation
  • Node document
  • Git
  • Run it first
  • Row-by-row Analysis
    • Node. js [5] connect & express Introduction
    • Node. js [4] First Module
    • Node. js [3] overlooking the API (coming soon)
    • Node. js [2] Hello Node
    • Node. js [1] overlooking NPM
    • Introduction to node. js [0]

The previous article looked down at NPM, and this time it was time to enter the topic of node development.

Prepare node Installation

Use the installation package for installation in windows. Other platforms also have the corresponding installation package or download the source code for installation. Use the following command to verify the installation. If everything goes well, you can see the node version number:

$ node -v
Node document

We recommend that you use an online version. If you need an offline version, you can clone the Node Code from GitHub and compile the complete API documentation in Linux.

$ git clone git://github.com/joyent/node.git
$ cd node
$ make doc
Git

Git is a distributed version control system. Unlike SVN's central control, git has a complete version library on each node. The biggest difference with SVN lies in branch management, this also creates two completely different version control styles. For more details about git, refer to the open-source git tutorial progit.

Msysgit is popular in windows; it can be directly installed in the Application Center in Ubuntu; other platforms are mostly installed through source code.

Although git has a large number of commands, the following are commonly used in the initial stage:

Git clone: copy a version library from the server (git is like this, the entire library will be pulled down)

Git Add: add local files to the local git repository (not submitted yet)

Git commit: submit all the modifications. Git add and git commit are usually merged:

$ git commit -a -m 'some comment'

Git push: pushes the local version Library to the server (usually GitHub)

$ git push –u master origin
Hello node: Run it first.

The demo below is from nodejs.org (with minor changes ):

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Save it as hello. JS, input

$ Node hello. js

Server running at http://127.0.0.1:1337/

A simple HTTP server is running.

http://127.0.0.1:1337/
Row-by-row Analysis
// Node uses JavaScript as its application-layer programming language, so the node program syntax is no different from that of Js.
// Declare the node custom module (HTTP)
var http = require('http');
// Create and start the HTTP server
http.createServer(
// The following callback is triggered upon each request
function (req, res) {
// Output HTTP Header
  res.writeHead(200, {'Content-Type': 'text/plain'});
// Output the HTTP body and complete the response operation
  res.end('Hello World\n');
// Listen for 127.0.0.1: 1337 requests,
}).listen(1337, '127.0.0.1');
// The console. log is used in the same way as the browser.
// The log is printed to the standard output (that is, the command line interface)
console.log('Server running at http://127.0.0.1:1337/');
A simple static Server

Here is a simple static file server code (Download complete demo). If you are interested, refer to the API documentation:

var
    http = require('http'),
    url = require('url'),
    fs = require('fs');
// Create and start the server
// Enter http: // 127.0.0.1: 3001/demo.html in the address bar of the browser
http.createServer(function (req, res) {
    var pathname = __dirname + '/public' + url.parse(req.url).pathname;
// Read local files
// One of the design concepts of node: each processing link of the Web server is asynchronous.
    fs.readFile(pathname, function (err, data) {
        if (err) {
            res.writeHead(500);
            res.end('500');
        } else {
// You can determine the file type and add content-type.
            res.end(data);
        }
    });
}).listen(3001, function () {
        console.log('http.Server started on port 3001');
    });

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.