JavaScript script on the server Node. js getting started _ javascript tips-js tutorial

Source: Internet
Author: User
Reached out to the legendary Server-sidemo-crpt. Backend JS is undoubtedly the node of RyanDahl. js, and jaxer developed by the aptanaIDE provider. Here we will discuss node. to use js, first download node. js, decompress it to the E disk, change it to node, start the menu, Enter cmd, 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:

The Code is as follows:


Var sys = require ("sys ");
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:

The Code is as follows:


Var sys = require ("sys "),
Http = require ("http ");
Http. createServer (function (request, response ){
Response. sendHeader (200, {"Content-Type": "text/html "});
Response. write ("Hello World! ");
Response. close ();
}). Listen (8080 );
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.

The Code is as follows:


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.

The Code is as follows:


// 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

The Code is as follows:


# Include int main () {printf ("Hello World !!! "); Exit (0 );}

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.