May 10, 2021 Node.js
Create a file called hello .js file:
/**
* comment.
*/
console.log("Hello World!");
Now we can execute this file from the command line
node hello.jsYou can see the following output:
Hello World!
Variables are defined using the var keyword in JavaScript. For example, the following snipple creates a variable foo and logs it to the console.
var myData = 123;
console.log(myData);
The code above produces the following results.
The JavaScript runtime has the opportunity to define some global variables that we can use in our code. O
ne of them is a console object.
The console object contains a member function (log) that accepts any number of parameters and prints them to the console.
var http = require("http");
/*from www.w3cschool.cn*/
function process_request(req, res) {
var body = 'Thanks for calling!\n';
var content_length = body.length ;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
res.end(body);
}
var s = http.createServer(process_request);
s.listen(8080);
To run it, just type
node web.jsYour computer now has a Web server running on port 8080. We can enter the information in the web browser http://localhost:8080.
Or use:
curl -i http://localhost:8080Now you should see something like this:
HTTP/1.1 200 OK Content-Length: 20 Content-Type: text/plain Date: Tue, 15 Feb 2013 03:05:08 GMT Connection: keep-alive Thanks for calling!
To start it, simply type in the command prompt or PowerShell:
C:\Users\abc\curl --help
C:\Users\abc\wget --help
node debug web.js