Node Js
Node Js
Why Node.js?
Node.js uses asynchronous programming!
A common task for a web server can be to open a file on the server and return
the content to the client.
Node.js eliminates the waiting, and simply continues with the next request.
Getting Started
Once you have downloaded and installed Node.js on your computer, let's try to
display "Hello World" in a web browser.
Create a Node.js file named "myfirst.js", and add the following code:
myfirst.js
How to open the command line interface on your computer depends on the
operating system. For Windows users, press the start button and look for
"Command Prompt", or simply write "cmd" in the search field.
Navigate to the folder that contains the file "myfirst.js", the command line
interface window should look something like this:
C:\Users\Your Name>_
Start your command line interface, write node myfirst.js and hit enter:
Initiate "myfirst.js":
If anyone tries to access your computer on port 8080, they will get a "Hello
World!" message in return!
Include Modules
To include a module, use the require() function with the name of the module:
Now your application has access to the HTTP module, and is able to create a
server:
The following example creates a module that returns a date and time object:
exports.myDateTime = function () {
return Date();
};
Use the exports keyword to make properties and methods available outside the
module file.
Example
Use the module "myfirstmodule" in a Node.js file:
Example
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(8080);
This object has a property called "url" which holds the part of the url that comes
after the domain name:
demo_http_url.js
var fs = require('fs');
Read files
Create files
Update files
Delete files
Rename files
Read Files
The fs.readFile() method is used to read files on your computer.
Assume we have the following HTML file (located in the same folder as Node.js):
demofile1.html
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
Create Files
The File System module has methods for creating new files:
fs.appendFile()
fs.open()
fs.writeFile()
The fs.appendFile() method appends specified content to a file. If the file does
not exist, the file will be created:
Example
Create a new file using the appendFile() method:
var fs = require('fs');
The fs.open() method takes a "flag" as the second argument, if the flag is "w"
for "writing", the specified file is opened for writing. If the file does not exist, an
empty file is created:
Example
Create a new, empty file using the open() method:
var fs = require('fs');
The fs.writeFile() method replaces the specified file and content if it exists. If
the file does not exist, a new file, containing the specified content, will be
created:
Example
Create a new file using the writeFile() method:
var fs = require('fs');
Update Files
The File System module has methods for updating files:
fs.appendFile()
fs.writeFile()
The fs.appendFile() method appends the specified content at the end of the
specified file:
Example
Append "This is my text." to the end of the file "mynewfile1.txt":
var fs = require('fs');
Example
Replace the content of the file "mynewfile3.txt":
var fs = require('fs');
Run example »
Delete Files
To delete a file with the File System module, use the fs.unlink() method.
Example
Delete "mynewfile2.txt":
var fs = require('fs');
Rename Files
To rename a file with the File System module, use the fs.rename() method.
Example
Rename "mynewfile1.txt" to "myrenamedfile.txt":
var fs = require('fs');
Parse an address with the url.parse() method, and it will return a URL object
with each part of the address as properties:
Run example »
Create two html files and save them in the same folder as your node.js files.
summer.html
<!DOCTYPE html>
<html>
<body>
<h1>Summer</h1>
<p>I love the sun!</p>
</body>
</html>
winter.html
<!DOCTYPE html>
<html>
<body>
<h1>Winter</h1>
<p>I love the snow!</p>
</body>
</html>
Create a Node.js file that opens the requested file and returns the content to
the client. If anything goes wrong, throw a 404 error:
demo_fileserver.js:
What is NPM?
NPM is a package manager for Node.js packages, or modules if you like.
The NPM program is installed on your computer when you install Node.js
What is a Package?
A package in Node.js contains all the files you need for a module.
Download a Package
Downloading a package is very easy.
Open the command line interface and tell NPM to download the package you
want.
Download "upper-case":
NPM creates a folder named "node_modules", where the package will be placed.
All packages you install in the future will be placed in this folder.
Using a Package
Once the package is installed, it is ready to use.
Include the "upper-case" package the same way you include any other module:
var uc = require('upper-case');
Create a Node.js file that will convert the output "Hello World!" into upper-case
letters:
Run example »
Save the code above in a file called "demo_uppercase.js", and initiate the file:
Initiate demo_uppercase: