Skip to content

Commit c3c3f30

Browse files
author
gevic
committed
Program samples added till FileServer concepts in Node.js
1 parent a0d00ec commit c3c3f30

10 files changed

+160
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# Node.js-tutorials
22
Node.js small code tutorials for Beginners.
3+
4+
## What is Node.js
5+
It is a an open source server enviroment which user JavaScript on the server. It uses asynchronous programming.
6+
7+
## Why Node.js
8+
Let's deal with it using an example. A common task on web server is to open a file on the server and return to the client.
9+
10+
Here is how PHP and ASP will handle it:
11+
* Sends the task to the computer's file system.
12+
* Waits while the file system opens and reads the file.
13+
* Returns the content to the client.
14+
* Ready to handle the next request.
15+
16+
Here is how Node.js handles a file request:
17+
* Sends the task to the computer's file system.
18+
* Ready to handle the next request.
19+
* When the file system has opened and read the file, the server returns the content to the client.
20+
21+
It eliminate the wait time here. Hence, is memory efficient.
22+
23+
## What can it do?
24+
* It can generate dynamic page content
25+
* It can create, open, read, write, delete, and close files on the server
26+
* It can collect form data
27+
* It can add, delete, modify data in your database
28+
29+
This intro part is taken from https://www.w3schools.com/nodejs/nodejs_intro.asp.
30+
31+

createModules.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Apart from built-in modules, you can define you own modules
2+
// Let's say our module returns date
3+
// {exports} keywors is used to make the function available outside the module file
4+
exports.getDateTime = function() {
5+
return Date();
6+
}

fileServer.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Node.js as File server
2+
// file system module in node.js allow you to work with the file system on our computer.
3+
var fileSystem = require('fs'); // can create, read, update delete, rename file
4+
var http = require('http');
5+
6+
// To read files i.e. fileSystem.readFile(). Say you have html file name operationTest.html
7+
http.createServer(function (req, res){
8+
fileSystem.readFile('operationTest.html', function (err, data){ // reads the entire content of the file asynchronously
9+
res.writeHead(200, {'Content-Type':'text/html'});
10+
res.write(data);
11+
res.end();
12+
});
13+
}).listen(8080)
14+
15+
// basicly you can run your html files using this
16+
17+
// Create files using appendFile (created if not present)
18+
fileSystem.appendFile('createFile.txt', 'Yupee created!', function(err){
19+
if(err) throw err;
20+
console.log('Saved!');
21+
});
22+
23+
// open method
24+
fileSystem.open('createFile.txt', 'w', function(err, file){
25+
if(err) throw err;
26+
console.log('opened!');
27+
})
28+
29+
// write file (replaces the file content if exist with the specified one, if not then new file is created)
30+
fileSystem.writeFile('createFile.txt', 'Is the content replaced?', function(err){
31+
if(err) throw err;
32+
console.log('Replaced!');
33+
})
34+
35+
// Delete files. Method used is unlink
36+
fileSystem.unlink('createFile.txt', function(err){
37+
if(err) throw err;
38+
console.log('Deleted!');
39+
})
40+
41+
// renaming files
42+
file.rename('createFile.txt', 'renamed.txt', function(err){
43+
if(err) throw err;
44+
console.log(err);
45+
})

fileServer2.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Since now we know how to use url module according to our needs, we will open file and return it
2+
// to client parsing the url requested for
3+
var http = require('http');
4+
var url = require('url');
5+
var readFile = require('fs');
6+
7+
// Creating the server
8+
http.createServer(function (req, res){
9+
var query = url.parse(req.url, true);
10+
var fileName ='.' + query.pathname;
11+
// read the file
12+
readFile.readFile(fileName, function(err, data){
13+
if(err){
14+
res.writeHead('400', {'Content-Type':'text/html'});
15+
return res.end("404 NOT FOUND"); // I would encourage you to remove the return keyword from both the places and try
16+
}
17+
res.writeHead('200', {'Content-Type':'text/html'});
18+
res.write(data);
19+
return res.end();
20+
})
21+
}).listen(8080);
22+

first.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// http is a built-in module here. Think of modules as the libraries in JavaScript.
2+
// Require function is reuired to include a module.
3+
var http = require('http');
4+
5+
// Using http module, web server is created
6+
http.createServer(function (req, res) {
7+
res.writeHead(200, {'Content-Type': 'text/html'});
8+
res.end('Hello World!');
9+
}).listen(8080);

operationTest.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<h1> Hello, there</h1>
4+
<p> What's going on here? Nothing node.js file system module test</p>
5+
</body>
6+
</html>

readQuery.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var http = require('http');
2+
3+
// create the server. req arguement here represent the request from the client, as an object
4+
http.createServer(function (req, res) {
5+
res.writeHead(200, {'Content-Type': 'text/html'});
6+
res.write(req.url); // reads the query and write it to the client
7+
res.end();
8+
}).listen(8080);
9+
10+
// if you run the program with localhost:8080/anyName
11+
// output should be '/anyName'

splitQuery.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var http = require('http');
2+
var url = require('url');
3+
4+
http.createServer(function(req, res){
5+
res.writeHead(200, {'Content-Type': 'text/html'});
6+
var query = url.parse(req.url, true).query;
7+
var text = query.year + " " + query.month;
8+
res.end(text);
9+
}).listen(8080);

urlModule.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var url = require('url');
2+
var address = 'http://localhost:8080/operationTest.htm?year=2017&month=february';
3+
var query = url.parse(address, true);
4+
5+
console.log(query.hostname); // returns 'localhost:8080'
6+
console.log(query.pathname); // returns '/operationTest.htm'
7+
console.log(query.search); // returns '?year=2017&month=february'
8+
9+
var queryData = query.query; // returns an object { year: 2017, month: 'february' }
10+
console.log(queryData.month); // returns 'february'

useOurModule.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// import http module
2+
var http = require('http');
3+
// file name
4+
var date = require('./createModules');
5+
6+
// this method is executed when someone tries to access the computer at port 8080
7+
http.createServer(function (req, res) {
8+
res.writeHead(200, {'content-Type': 'text/html'}); // if the response supposed to be displayed as HTML
9+
res.write("Date and time is : " + date.getDateTime()); // Writes the response to the client
10+
res.end();// end the response
11+
}).listen(8080); // listen to the port at 8080
12+
13+
// remove the writeHead and you will be able to observe the difference in the text

0 commit comments

Comments
 (0)