Getting started with node. js

Source: Internet
Author: User

Dom framework cannot be released as scheduled because it runs to another city and has no computers. In this period, the most urgent need was to seek a super lightweight backend to build up my framework, so I reached out to the legendary server-side javascrpt. Backend JS is undoubtedly the node. js of Ryan Dahl, and jaxer developed by the Aptana ide provider.

Http://github.com/tlrobinson/narwhal

First download node. JS, decompress it to the E disk, change it to node, then Enter cmd in the menu, and use the CD command to switch to the decompressed directory of nodejs:

Example 1: Hello world.

Create a hello. js file in the node directory, and then enter:

 
// Var sys = require ("sys"); var sys = require ("util") SYS. Puts ("Hello World ");

Then, enter the command in the nameboard.Node hello. jsThe output result of the nameboard Hello world is displayed.

Example 2: Hello World2.

Okay. This time we try to output Hello world from the browser. Create HTTP. js in the node directory and enter:

// Var sys = require ("sys"); var sys = require ("util") var HTTP = require ("HTTP"); http. createserver (function (request, response) {// response. sendheader (200, {"Content-Type": "text/html"}); response. writeheader (200, {"Content-Type": "text/html"}); response. write ("Hello world! "); // Response. close (); response. end ();}). listen (1, 8080); sys. puts ("server running at http: // localhost: 8080 /");

Then, enter the command in the nameboard.Node HTTP. jsIn the browser, enterHttp: // localhost: 8080/

Example 3: Hello World2.

Node. js provides a buffer class for converting strings of different encodings. Currently, three types are supported: 'ascii ', 'utf8', and 'binary '. For details, see here.

VaR buffer = require ('buffer '). buffer, Buf = new buffer (256), Len = Buf. write ('\ u00bd + \ u00bc = \ u00be', 0); console. log (LEN + "Bytes:" + Buf. tostring ('utf8', 0, Len ));

Example 4: Hello world3.

 
// Synopsis. JS // synopsis abstract, synopsis, outline var HTTP = require ('http'); http. createserver (function (request, response) {response. writehead (200, {'content-type': 'text/plain '}); response. end ('Hello world \ n ');}). listen (8124); console. log ('server running at http: // 127.0.0.1: 8124 /');

Front-end address bar:Http: // localhost: 8124/

Example 5: compile a c file

# Include
# Include

Int main (){
Printf ("Hello world !!! ");
Exit (0 );
}

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.