Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Introduction .js Node


May 10, 2021 Node.js


Table of contents


Node .js a platform for writing network systems and Web applications, built around an event-driven, non-blocking programming model.
Run .js and Hello World!

Create a file called hello .js file:

/**
  * comment.
  */
console.log("Hello World!");

Now we can execute this file from the command line

node hello.js
You can see the following output:

Hello World!

Variable

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.

Introduction .js Node
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.

The first Web server

Enter and save the following to a file .js web:

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.js
Your 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:8080
Now 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!

curl

We can download curl http://curl.haxx.se/download.html binary by visiting the website and looking for the Win32 - Generic section. Download a highlighted binary, preferably SSL and SSH, unzip it, and place the curl .exe in PATH or the user directory.

To start it, simply type in the command prompt or PowerShell:

C:\Users\abc\curl --help

Wget

wget is a good choice for curl. We can download http://users.ugent.be/~bpuype/wget/ the website.
To learn more, see Help:

C:\Users\abc\wget  --help

Attention

To stop the server from running, simply press Ctrl-C, which is enough to clean up everything and shut it down properly.
To debug, simply add a debug flag before the program name:

node debug web.js