Last active
November 24, 2020 14:40
-
-
Save hagevvashi/af8b177b9b370585602320016c3f98f5 to your computer and use it in GitHub Desktop.
Node.js minimum server implementation in TypeScript
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
import http from "http"; | |
import url from "url"; | |
const [, , flag, port] = process.argv; | |
if (flag !== "-p") throw new Error("Error! must specify port number."); | |
const server = http.createServer(); | |
server.on("request", (req, res) => { | |
const { pathname } = url.parse(req.url); | |
if (!pathname) throw new Error("Error! pathname is null."); | |
res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" }); | |
res.write(` | |
<!doctype html> | |
<html> | |
<head> | |
<title>Hello World</title> | |
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1"/> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<div id="app"></div> | |
</body> | |
</html> | |
`); | |
res.end(); | |
}); | |
server.listen(port); | |
process.stdout.write("server start.\n"); | |
process.stdout.write(`Available on ${port}\n`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
update