Node. js: hello world

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.

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:

 
 
  1. var sys = require("sys");  
  2. sys.puts("Hello world"); 

Then, enter the node hello. js command in the nameboard to view the output result Hello world.

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:

 
 
  1. var sys = require("sys"),  
  2.     http = require("http");  
  3. http.createServer(function(request, response) {  
  4.     response.sendHeader(200, {"Content-Type": "text/html"});  
  5.     response.write("Hello World!");  
  6.     response.close();  
  7. }).listen(8080);  
  8. sys.puts("Server running at http://localhost:8080/");  

Then, enter the command node http. js in the naming stage, and enter http: // localhost: 8080/in the browser/

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.

 
 
  1. var Buffer = require('buffer').Buffer,  
  2. buf = new Buffer(256),  
  3. len = buf.write('\u00bd + \u00bc = \u00be', 0);  
  4. console.log(len + " bytes: " + buf.toString('utf8', 0, len)); 

Example 4: hello world3.

 
 
  1. // Synopsis. js
  2. // Synopsis abstract, synopsis, outline
  3. Var http = require ('http ');
  4. Http. createServer (function (request, response ){
  5. Response. writeHead (200, {'content-type': 'text/plain '});
  6. Response. end ('Hello World \ n ');
  7. }). Listen (8124 );
  8. Console. log ('server running at http: // 127.0.0.1: 8124 /');

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

Source: http://www.cnblogs.com/rubylouvre/

Related Article

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.