Created
April 8, 2020 07:08
-
-
Save annibuliful/1d0c74175b62d98819f32566f2637d91 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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