Skip to content

Instantly share code, notes, and snippets.

@annibuliful
Created April 8, 2020 07:08
Show Gist options
  • Save annibuliful/1d0c74175b62d98819f32566f2637d91 to your computer and use it in GitHub Desktop.
Save annibuliful/1d0c74175b62d98819f32566f2637d91 to your computer and use it in GitHub Desktop.
const http = require("http");
const fs = require("fs");
const { promisify } = require("util");
// convert callback To Async
// const readAsync = promisify(fs.readFile);
// convert CB to Async with promise
const readAsync = fileName => {
return new Promise((resolve, reject) => {
fs.readFile(fileName, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
};
const server = async () => {
const html = await readAsync("index.html");
console.log(html.length);
http
.createServer((req, res) => {
res.writeHead(200, {
"Content-Length": html.length,
"Content-Type": "text/html"
});
res.end(html);
})
.listen(3030);
};
server();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment