Built in Node Js Modules
Tags: javascript, web, programming, beginners
Published: 2021-11-13 20:05:18

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](https://media.giphy.com/media/1dc3zaVLsN2K6cglOd/giphy.gif)

## 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

<kbd>Exporting modules</kbd>
```Javascript
module.exports.<function name> = <function name>
module.exports.sayName = sayName;
```
<kbd>Importing modules</kbd>
```Javascript
const fs = require('fs');
```
---

## <mark>Path module</mark>: To handle file paths
<kbd>Joining file path with files</kbd>
```Javascript
Ex1.
const path = require('path');
const fileLocation = path.join(__dirname, 'app.js')
console.log(fileLocation);
```

---

```Javascript
Ex2.
const path = require('path');
const fileLocation = path.join(__dirname, '../user-model.js')
console.log(fileLocation);
```

---

<kbd>Getting Base name</kbd>
```Javascript
const path = require('path');
const fileLocation = path.join(__dirname, '../user-model.js')
const fileName = path.basename(fileLocation)
console.log(fileName);
```

---

## <mark>fs module</mark>:To handle the file system
```Javascript
const path = require('path');
const fileLocation = path.join(__dirname, '../user-model.js')
const fileName = path.extname(fileLocation)
console.log(fileName);
```

---

## <mark>url module</mark>: To parse URL strings 
```Javascript
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);
```

---

## <mark>fs module</mark>: To handle the file system
<kbd>Read file</kbd>
```Javascript
const fs = require('fs');
fs.writeFile('note.txt', 'hey I just wrote my first code', err =>{
  if(err) throw err;
  console.log('File written.....');
})
```

---

<kbd>Read file</kbd>
```Javascript
const fs = require('fs');
fs.readFile('note.txt', 'utf8', (error, response)=>{
  if(error) throw error;
  console.log(response);
})
```

---

## <mark>http module</mark>: To make Node.js act as an HTTP server
```Javascript
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.....'));
```

---

Ex2
```Javascript
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.....'));
```

##<mark> Crypto module</mark>:To handle OpenSSL cryptographic functions
```Javascript
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); 
```

---

## 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 <mark>hashing</mark>, 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](https://nodejs.org/dist/latest-v16.x/docs/api/) for more.
Hope you found this post useful. Thanks for reading.

## Reference
[w3s](https://www.w3schools.com/nodejs/ref_modules.asp)
[Dev Ed](https://www.youtube.com/watch?v=zQRrXTSkvfw)