Experiment 5
Experiment 5
THEORY:
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.
Built-in Modules
Node.js has a set of built-in modules which you can use without any further installation.
Node.js has a set of built-in modules which you can use without any further installation.
Module Description
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:
res.end('Hello World!');
}).listen(8080);
You can create your own modules, and easily include them in your applications.
The following example creates a module that returns a date and time object:
Example
exports.myDateTime = function () {
return Date();
};
Use the exports keyword to make properties and methods available outside the module file.
App.js
Notice that we use ./ to locate the module, that means that the module is located in the same folder
as the Node.js file.
Save the code above in a file called "demo_module.js", and initiate the file:
Initiate demo_module.js:
If you have followed the same steps on your computer, you will see the same result as the example:
http://localhost:8080
EXERCISE: