Node.js Modules

What is Node.js Modules ?

Node.js Modules is a set of functions which you can include in your application. Node.js Modules is same as JavaScript libraries.

Node.js provides many built-in modules like http, express fs, path, events, string, moments, events, os , url  etc.  

We can include all Node.js module in our applicaton using require() function.

How to access Node.js module?

You can access module using require() function as below.

const http = require('http');

const port = 5555;

const server = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Welcome to aryatechno tutorials!');
}).listen(8080);

server.listen(port function() {

    console.log(`Server running at port ${port}: http://127.0.0.1:${port}`)

})

 

How to create our own modules?

We can create our own modules using exports keyword as below.

exports.moduleName = async function (req, res, next) {

return data;

}

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

41188