Raw Node

Exporting and Exporting modules

module.exports.<function name> = <function name>
module.exports.sayName – sayNane;

Joining path

Ex1.
const path = require(‘path’);
const fileLocation = path.join(__dirname…


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi

Exporting and Exporting modules

module.exports.<function name> = <function name>
module.exports.sayName - sayNane;

Joining path

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

Getting Base name

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

File Extention

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

Working with urls

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);

Write file using the fs module

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

Read file

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

Working with http module.

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

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

Hashing passwords with crypto

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); 

Decrypt and Encrytp with crypto

Encrypt

const crypto = require('crypto');
const algorithm = 'aes-192-cbc';

const password ='fygfudgfdfy';
const key = crypto.scryptSync(password, 'salt', 64);
const cipher = crypto.createCipher(algorithm, key);

let encryptedMessage = '';
cipher.on('readable', ()=>{
  let chunk;
  while(null !== (chunk = cipher.read())){
    encryptedMessage += chunk.toString('hex')
  }
})
cipher.on('end', ()=> console.log(encryptedMessage));
cipher.write('this is my hidden message');
cipher.end();

Decrypt

const crypto = require('crypto');
const algorithm = 'aes-192-cbc';

const password ='fygfudgfdfy';
const key = crypto.scryptSync(password, 'salt', 64);
const decipher = crypto.createDecipher(algorithm, key);

let decryptedMessage = '';
decipher.on('readable', ()=>{
  while(null !== (chunk = decipher.read())){
    decryptedMessage += chunk.toString('utf8')
  }
})
decipher.on('end', ()=> console.log(decryptedMessage));
const encrypted = '0c168352c32ac183356e643c4696c9c535380a5d58158764649bb7cb259ec679' // gotten from above encryption
decipher.write(encrypted, 'hex');
decipher.end();


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi


Print Share Comment Cite Upload Translate Updates
APA

Abayomi Ogunnusi | Sciencx (2021-11-13T20:05:18+00:00) Raw Node. Retrieved from https://www.scien.cx/2021/11/13/raw-node/

MLA
" » Raw Node." Abayomi Ogunnusi | Sciencx - Saturday November 13, 2021, https://www.scien.cx/2021/11/13/raw-node/
HARVARD
Abayomi Ogunnusi | Sciencx Saturday November 13, 2021 » Raw Node., viewed ,<https://www.scien.cx/2021/11/13/raw-node/>
VANCOUVER
Abayomi Ogunnusi | Sciencx - » Raw Node. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/13/raw-node/
CHICAGO
" » Raw Node." Abayomi Ogunnusi | Sciencx - Accessed . https://www.scien.cx/2021/11/13/raw-node/
IEEE
" » Raw Node." Abayomi Ogunnusi | Sciencx [Online]. Available: https://www.scien.cx/2021/11/13/raw-node/. [Accessed: ]
rf:citation
» Raw Node | Abayomi Ogunnusi | Sciencx | https://www.scien.cx/2021/11/13/raw-node/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.