Created
September 18, 2021 09:06
-
-
Save adeisbright/b281b41f315574f9725b7cff59bf03e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createServer } = require("http"); | |
const fs = require("fs"); | |
const fsp = require("fs").promises; | |
let path = require("path"); | |
const { parse } = require("querystring"); | |
/* | |
The querystring module provides utilities for parsing and formatting url query strings | |
The parse method of the query string parses a URL query string into a collection of keys and value pairs. | |
For example , the query string 'name=adeleke&[email protected]' is parsed into | |
{ | |
name : 'adeleke' , | |
email : '[email protected]' | |
} | |
*/ | |
const PORT = 8000; | |
const CACHE = {}; | |
//For express , add the server caching in a middleware | |
const grabRequestBody = async (req, cb) => { | |
const FORM_ENCODINGS = [ | |
"application/x-www-form-urlencoded", | |
"multipart/form-data", | |
]; | |
const { headers } = req; | |
if (FORM_ENCODINGS.includes(headers["content-type"])) { | |
let body = ""; | |
req.on("data", (chunk) => { | |
body += chunk.toString(); | |
}); | |
req.on("error", (error) => { | |
throw new Error(error.message); | |
}); | |
req.on("end", () => { | |
cb(parse(body)); | |
}); | |
} else { | |
cb(null); | |
} | |
}; | |
const requestListener = async (req, res) => { | |
let { method, url } = req; | |
if (url === "/") { | |
res.writeHead(200, { "Content-Type": "text/html" }); | |
res.write("<h1>Welcome to Bigjara</h1>"); | |
res.write("<p>We are a startup focused on Internet Technologies</p>"); | |
res.end(); | |
} else if (url === "/sign-up") { | |
if (method === "GET") { | |
try { | |
let fileContent = CACHE["signUp"]; | |
if (fileContent) { | |
console.log(fileContent); | |
res.writeHead(200, { "Content-Type": "text/html" }); | |
res.end(fileContent); | |
} else { | |
let fileName = "sign-up.html"; | |
let filePath = path.join(__dirname, fileName); | |
console.log("Not from cache"); | |
fileContent = await fsp.readFile(filePath); | |
CACHE["signUp"] = fileContent; | |
res.writeHead(200, { "Content-Type": "text/html" }); | |
res.end(fileContent); | |
} | |
} catch (error) { | |
res.writeHead(500, { "Content-Type": "text/html" }); | |
res.write(`<h1>${error.message}</h1>`); | |
res.end(); | |
} | |
} else if (method === "POST") { | |
grabRequestBody(req, (result) => { | |
console.log(result); | |
let bodyContent = JSON.stringify(result); // Strinify the req body object | |
let parseContent = JSON.parse(bodyContent); | |
let { email, password, username } = parseContent; | |
let newFile = fsp.appendFile("users.txt", username + "\n"); | |
if (newFile) console.log("New name added"); | |
//res.writeHead(200 , {'content-type' : 'application/json'}) | |
let messages = ["hi", "hello", "paul"]; | |
messages.map((message) => res.write(`<p>${message}</p>`)); | |
res.write("<h1>Registration was successful</h1>"); | |
res.write( | |
`<p>Hello ${username} , your email is ${email} and password is ${password}</p>` | |
); | |
res.end(); | |
}); | |
} | |
} else if (url === "/about") { | |
fs.readFile("about-bigjara.html", (err, data) => { | |
if (err) { | |
res.writeHead(500, { "Content-Type": "text/plain" }); | |
res.write("Error 500 : Internal Server Error"); | |
res.end(); | |
} else { | |
res.writeHead(500, { "Content-Type": "text/html" }); | |
res.write(data); | |
res.end(); | |
} | |
}); | |
} else { | |
res.statusCode = 404; | |
res.setHeader("Content-Type", "text/html"); | |
res.write("<h1>Error 404</h1>"); | |
res.write("<p>Return Home</p>"); | |
res.end(); | |
} | |
}; | |
createServer(requestListener).listen(PORT, () => | |
console.log(`Your request is Listening on localhost:${PORT}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment