Skip to content

Commit 05d52f7

Browse files
Add files via upload
0 parents  commit 05d52f7

File tree

9 files changed

+4062
-0
lines changed

9 files changed

+4062
-0
lines changed

app.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var http = require('http');
2+
var fs = require('fs');
3+
var url = require('url');
4+
5+
// Create a server
6+
http.createServer( function (request, response) {
7+
// Parse the request containing file name
8+
var pathname = url.parse(request.url).pathname;
9+
10+
// Print the name of the file for which request is made.
11+
console.log("Request for " + pathname + " received.");
12+
13+
// Read the requested file content from file system
14+
fs.readFile(pathname.substr(1), function (err, data) {
15+
if (err) {
16+
console.log(err);
17+
18+
// HTTP Status: 404 : NOT FOUND
19+
// Content Type: text/plain
20+
response.writeHead(404, {'Content-Type': 'text/html'});
21+
} else {
22+
//Page found
23+
// HTTP Status: 200 : OK
24+
// Content Type: text/plain
25+
response.writeHead(200, {'Content-Type': 'text/html'});
26+
27+
// Write the content of the file to response body
28+
response.write(data.toString());
29+
}
30+
31+
// Send the response body
32+
response.end();
33+
});
34+
}).listen(8081);
35+
36+
// Console will print the message
37+
console.log('Server running at http://127.0.0.1:8081/');

cllient.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var http = require('http');
2+
3+
// Options to be used by request
4+
var options = {
5+
host: 'localhost',
6+
port: '8081',
7+
path: '/index.htm'
8+
};
9+
10+
// Callback function is used to deal with response
11+
var callback = function(response) {
12+
// Continuously update stream with data
13+
var body = '';
14+
response.on('data', function(data) {
15+
body += data;
16+
});
17+
18+
response.on('end', function() {
19+
// Data received completely.
20+
console.log(body);
21+
});
22+
}
23+
// Make a request to the server
24+
var req = http.request(options, callback);
25+
req.end();

demofile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<h1>bhavesh</h1>
5+
<p>bhavesh!</p>
6+
</body>
7+
</html>

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var http = require('http');
2+
var dt = require('./myfirstmodule');
3+
var dt = require('./p.py');
4+
var dt = require('./server.js');
5+
var dt = require('./app.js');
6+
var fs = require('fs');
7+
http.createServer(function (req, res) {
8+
//Open a file on the server and return its content:
9+
fs.readFile('index.html', function(err, data) {
10+
res.writeHead(200, {'Content-Type': 'text/html'});
11+
res.write(data);
12+
return res.end();
13+
});
14+
}).listen(8080);

myfirstmodule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.myDateTime = function () {
2+
return Date();
3+
};

0 commit comments

Comments
 (0)