Ubuntu system uses Node.js to run Hello world method

  

Node.js provides a rich JavaScript library for various Linux modules, which simplifies the use of Node.js, a great convenience for web applications. Research and development. This article will introduce one of the most basic knowledge of Node.js, which is to run Hello world in Ubuntu. After

installed node.js, any path in the new file helloworld.js, enter the following:

console.log (! & Ldquo; Hello world & rdquo;)

Then start Terminal in the path (Command Line Command Line in Windows), or start Terminal and then use cd command to switch to the path (Windows equivalent), then enter the command

node Helloworld.js

Enter, the window will output “Hello world! & rdquo; typeface.

The above is the simplest hello world program for node. Node.js as a server programming technique, another basic hello world program is to output Hello world on the web page.

Change the content of helloworld.js to the following content:

var http =require ("http”);//Get http object

//Create a server object using the createServer function of the http object. The parameter is a no-argument function. The function uses the response object to write the header information to the browser. Text

var server = http.createServer(function(request, response) {

response.writeHead(200, {“Content-Type”:“text/plain”});

response.write(“Hello World”);

response.end();

});

//server object listens 8888 Port

server.listen(8888);

Then execute node helloworld.js and then visit http://localhost:8888/

in the browser It will show Hello world!

The Ubuntu system uses Node.js to run Hello World. Just learn this trick and start getting started with Node.js.

Copyright © Windows knowledge All Rights Reserved