Experiment 8
Experiment 8
THEORY:
● Server − This layer has the Web server which can intercept the
interacts with the data layer via the database or some external
programs.
data.
File: server.js
var fs = require('fs');
// Create a server
Page 2 of 7
// Print the name of the file for which request is made.
if (err) {
console.log(err)
} else {
//Page found
response.write(data.toString());
response.end();
Page 3 of 7
});
}).listen(8081);
Next let's create the following html file named index.htm in the same directory where
you created server.js.
File: index.htm
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>
$ node server.js
Page 4 of 7
Make a request to Node.js server
File: client.js
var options = {
host: 'localhost',
port: '8081',
path: '/index.html'
};
response.on('data', function(data) {
body += data;
Page 5 of 7
});
response.on('end', function() {
console.log(body);
});
req.end();
Now run the client.js from a different command terminal other than
$ node client.js
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
Page 6 of 7
</body>
</html>
EXERCISE:
EVALUATION:
Understanding /
Involvement Timely
Problem solving Total
Completion
(10)
(4) (3)
(3)
Page 7 of 7