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:
Copy codeThe 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:
Copy codeThe 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.
Copy codeThe 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.
Copy codeThe 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
Copy codeThe Code is as follows:
# Include int main () {printf ("Hello World !!! "); Exit (0 );}