DEV Community

Cover image for Built in Node Js Modules
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Built in Node Js Modules

This is a brief post with an example on how to use in-built modules in NodeJs. We're only going to look at a few.

started

Prerequisite

🥦 Install NodeJs
🥦 Nodejs knowledge is required.


TOC

♣️ Exporting and Importing modules
♣️ Path Module
♣️ url module
♣️ file System module
♣️ HTTP module
♣️ Crypto Module

Import and Export modules

Exporting modules

module.exports.<function name> = <function name>
module.exports.sayName = sayName;
Enter fullscreen mode Exit fullscreen mode

Importing modules

const fs = require('fs');
Enter fullscreen mode Exit fullscreen mode

Path module: To handle file paths

Joining file path with files

Ex1.
const path = require('path');
const fileLocation = path.join(__dirname, 'app.js')
console.log(fileLocation);
Enter fullscreen mode Exit fullscreen mode

Ex2.
const path = require('path');
const fileLocation = path.join(__dirname, '../user-model.js')
console.log(fileLocation);
Enter fullscreen mode Exit fullscreen mode

Getting Base name

const path = require('path');
const fileLocation = path.join(__dirname, '../user-model.js')
const fileName = path.basename(fileLocation)
console.log(fileName);
Enter fullscreen mode Exit fullscreen mode

fs module:To handle the file system

const path = require('path');
const fileLocation = path.join(__dirname, '../user-model.js')
const fileName = path.extname(fileLocation)
console.log(fileName);
Enter fullscreen mode Exit fullscreen mode

url module: To parse URL strings

const url = require('url');

const getUrlAddress = 'https://localhost:3003/users?key=abayomi&age=24';
const parsedUrl = url.parse(getUrlAddress, true);
console.log(parsedUrl);
console.log(parsedUrl.href);
console.log(parsedUrl.path);
console.log(parsedUrl.search);
console.log(parsedUrl.port);
console.log(parsedUrl.pathname);
console.log(parsedUrl.query);
console.log(parsedUrl.query.age);
Enter fullscreen mode Exit fullscreen mode

fs module: To handle the file system

Read file

const fs = require('fs');
fs.writeFile('note.txt', 'hey I just wrote my first code', err =>{
  if(err) throw err;
  console.log('File written.....');
})
Enter fullscreen mode Exit fullscreen mode

Read file

const fs = require('fs');
fs.readFile('note.txt', 'utf8', (error, response)=>{
  if(error) throw error;
  console.log(response);
})
Enter fullscreen mode Exit fullscreen mode

http module: To make Node.js act as an HTTP server

const http = require('http');
const server = http.createServer((req,res)=>{
  if(req.url === '/'){
    res.writeHead(200, {'Content-Type':'text/html'})
        res.write('<h1> Hey gimme ma money...dude!!</h1>')
        res.end();
  }

});

server.listen(4545, ()=> console.log('Server is crawling.....'));
Enter fullscreen mode Exit fullscreen mode

Ex2

const http = require('http');
const fs = require('fs');

const server = http.createServer((req,res)=>{
  if(req.url === '/'){
    fs.readFile('note.txt', (error, response)=>{
    res.writeHead(200, {'Content-Type':'text/html'})
        res.write(response)
        res.end();
    })
  }

});

server.listen(4545, ()=> console.log('Server is crawling.....'));
Enter fullscreen mode Exit fullscreen mode

Crypto module:To handle OpenSSL cryptographic functions

Ex1. Encrytping
const crypto = require('crypto');

const hashedMessage = crypto.createHash('md5').update('This is our secret').digest('hex')
console.log(hashedMessage);

Ex2. using sha256
const crypto = require('crypto');

const secretKey = 'pinkyandthebrain';
const hashedMessage = crypto.createHmac('sha256', secretKey).update('this is our secret').digest('hex')

console.log(hashedMessage); 
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is the most basic thing you can accomplish with NodeJs; there are packages like bcrypt that may be used in place of crypto for hashing, and Express Js for developing server-based APIs. It's essential that we grasp the fundamentals.
To acquire a list of the built modules that aren't included here, such as os and assert, and so on. Click Node Docs for more.
Hope you found this post useful. Thanks for reading.

Reference

w3s
Dev Ed

Top comments (0)