How to create Node.js Module

How to create Node.js Module

Node.js has a set of modules that are built in.Example of these built-in modules are.

  1.http-creates http server.
  2.fs-to handle file system.
  3.assert-provide assertions.


Example code on how to use http module.

Below code creates a server to displays "Programming in Node.js"  string.

var http=require('http');
http.createServer(function(req,res)
{
res.writeHead(200,{'Content-type':'text/plain'});
res.write("Programming in Node.js");
res.end();

}).listen(8080);

Apart from inbuilt node.js modules,developers can create their own modules and use them anywhere in the project and this makes the code reusable.

Creating a node.js  module to return current date and time

var op=require('os');
exports.myOperatingSystem=function()
{
return op.platform();
}

save above code as "firstmodule.js"

var http=require('http');

var os=require('./firstmodule');

http.createServer(function(req,res)
{
res.writeHead(200,{'Content-type':'text/html'});
res.write("My Operating system is:" + os.myOperatingSystem() );
res.end();

}).listen(8080);

save the code as" firstscript.js"

Open a command prompt and run the file firstscript file.



Output




NB:
Any time you make changes to a node.js file, the changes don't reflect automatically to the server.To avoid restarting the server whenever making changes,

Try to Install 


1. Nodemon  or 


2.Supervisor 



COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: How to create Node.js Module
How to create Node.js Module
How to create Node.js Module
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSRg2J5Yzh1kn48vTglbuCSMj7uXHr6xUoNdrW2UiGRnOffKyuRup9saM_3OFKcbe7bjg1KufCZzF5ZzbY-0Z4maEgYZyqrUwv_Q5yR0kY6lNyqQB-YRqQkqO7r0a85km_h6-ynUdE1oBj/s320/output.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSRg2J5Yzh1kn48vTglbuCSMj7uXHr6xUoNdrW2UiGRnOffKyuRup9saM_3OFKcbe7bjg1KufCZzF5ZzbY-0Z4maEgYZyqrUwv_Q5yR0kY6lNyqQB-YRqQkqO7r0a85km_h6-ynUdE1oBj/s72-c/output.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2018/02/how-to-create-nodejs-module.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2018/02/how-to-create-nodejs-module.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy