Professional Documents
Culture Documents
js Introduction
Node.js is a platform for writing network and web applications.
Node.js is built around an event-driven nonblocking model of programming.
Running Node.js and "Hello World!"
Create a file called hello.js:
/**
* comment.
*/
console.log("Hello World!");
Now, we can execute this file from the command line with
node hello.js
Variables
Variables are defined in JavaScript using the var keyword.
For example, the following code segment creates a variable foo and logs it to
the console.
The JavaScript runtime has the opportunity to define a few global variables
that we can use in our code.
One of them is the console object.
The console object contains a member function (log), which takes any number
of arguments and prints them to the console.
or use
curl -i http://localhost:8080
curl
We can download the Windows binaries for curl by visiting
http://curl.haxx.se/download.html and looking there for the "Win32 - Generic"
section.
Download one of the highlighted binaries, preferably one with support for SSL
and SSH, unpack it, and put curl.exe somewhere in your PATH or user
directory.
To launch it, in the command prompt or PowerShell, just type
C:\Users\abc\curl --help
Wget
wget is a great alternative for curl.
We can download it from http://users.ugent.be/~bpuype/wget/.
To learn more, view the help:
C:\Users\abc\wget --help
Note
To stop the server from running, you simply press Ctrl+C.
It is smart enough to clean up everything and shut down properly.
To debug, just add the debug flag before the name of your program:
node debug web.js
Example
Node.js uses a single thread to handle requests.
function longRunningOperation(callback) {
// simulate a 3 second operation
function userClicked() {
console.log('starting a long operation');
longRunningOperation(function () {
console.log('ending a long operation');
});
}
// simulate a user action
userClicked();
var x;
console.log(x);
Null
null is an explicit assertion that there "is no value":
var y;
console.log(y);
y = null ;
console.log(y);
typeof
To see the type of anything in JavaScript, use the typeof operator:
console.log(typeof 10);
console.log(typeof "hello");
console.log(typeof function () { var x = 20; });
Constants
The standard practice is to use uppercase letters and variable declarations:
JavaScript has both the equality operator == and the precise equality operator
===.
console.log(234 == '234');
console.log(234 === '234');
console.log(234234.235235 == 'cat');
console.log("cat" == "CAT");
console.log("cat".toUpperCase() == "CAT");
function fine(param) {
if (param == null || param == undefined || param == '')
throw new Error("Invalid Argument");
}
function better(param) {
if (!param)
throw new Error("Invalid Argument");
}
var x = 234;
var x1 = new Number(234);
console.log(typeof x);
console.log(typeof x);
console.log(x1 == x);
console.log(x1 === x);
N
ode.js Numbers