diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml new file mode 100644 index 0000000..8cf7c31 --- /dev/null +++ b/pnpm-lock.yaml @@ -0,0 +1,23 @@ +lockfileVersion: '9.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +importers: + + .: + dependencies: + colors: + specifier: ^1.4.0 + version: 1.4.0 + +packages: + + colors@1.4.0: + resolution: {integrity: sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==} + engines: {node: '>=0.1.90'} + +snapshots: + + colors@1.4.0: {} diff --git a/samoina/crypto-module.mjs b/samoina/crypto-module.mjs new file mode 100644 index 0000000..8f7ed43 --- /dev/null +++ b/samoina/crypto-module.mjs @@ -0,0 +1,51 @@ +import { createServer } from 'node:http'; +import { createHash } from 'node:crypto'; +import colors from 'colors'; + +const port = 3000; + +const server = createServer((req, res) => { + if (req.method === 'POST' && req.url === '/password') { + let data = ''; + + //data sent to the server arrives in chunks and .on()is a method that allows us to listen for data being sent to the server. all of the data is stored in the data variable + req.on('data', (chunk) => { + data += chunk; + }); + + //when the response is complete, get the PW, validate it nd hash it + req.on('end', () => { + const userPassword = JSON.parse(data).password; + + if (!userPassword) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ message: 'Password is required' })); + return; + } + + if (userPassword.length < 5) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end( + JSON.stringify({ message: 'Password must be at least 5 characters' }) + ); + return; + } + + //Hashing the user password + //1. createHash uses the sha256 algorithm to hash the password others are like 'sha1', 'md5', etc + //2. update() takes the password and updates it with the sha256 algorithm + //3. digest() returns the hashed password specifying that it should be in hex + const hash = createHash('sha256').update(userPassword).digest('hex'); + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ message: 'Password is valid.', hash })); + }); + } else { + res.writeHead(404, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ message: 'Not found' })); + } +}); + +server.listen(port, '127.0.0.1', () => { + console.log(`Server is running on port ${port}`.yellow); +}); diff --git a/samoina/notes.md b/samoina/notes.md new file mode 100644 index 0000000..646f41a --- /dev/null +++ b/samoina/notes.md @@ -0,0 +1,23 @@ +# Mon 09 Dec task + +Use any of Node's inbuilt modules and use it in your own file + +## What are some of node's inbuilt modules? + +Node.js has several inbuilt modules,including; + +- os, fs, path, http + +we already used the http module in the first two sessions of 'How-To-Node' + +- os provides information about the operating system. + +### Crypto Module + +- crypto module performs cfryptographic operations eg hashing data, encryption and decryption and creating random numbers + +#### Hashing + +This is a method that changes plain text to encrypted (cipher) text. It is important to note that this is a one way function, so it is not possible to get the original text from the hashed value, but it is possible to regenerate it with the original text. This is what makes it ideal for managing passwords and login credentials.(this is different from encryption and decryption) + +When coming up with the code for the crypto password usage, I read that Node.js handles HTTP requests and responses as streams, which are divided into smaller chunks to make processing data faster and more memory efficient. diff --git a/samoina/os-module.mjs b/samoina/os-module.mjs new file mode 100644 index 0000000..a48131e --- /dev/null +++ b/samoina/os-module.mjs @@ -0,0 +1,20 @@ +import { createServer } from 'node:http'; +import os from 'node:os'; +import colors from 'colors'; + +const port = 3000; + +const server = createServer((req, res) => { + console.log('Request received'); + res.writeHead(200, { + 'Content-type': 'text/plain', + }); + res.end(`Hello there, your operating system is ${os.platform()}`); +}); + +server.listen(port, '127.0.0.1', () => { + console.log(`Server is running on port ${port}`.yellow); + console.log(`Hello there, your operating system is ${os.platform()}`); +}); + +