Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hagevvashi/af8b177b9b370585602320016c3f98f5 to your computer and use it in GitHub Desktop.
Save hagevvashi/af8b177b9b370585602320016c3f98f5 to your computer and use it in GitHub Desktop.
Node.js minimum server implementation in TypeScript
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`);
@hagevvashi
Copy link
Author

update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment