Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hagevvashi/23026b304a1f061a13a5868590ce9f70 to your computer and use it in GitHub Desktop.
Save hagevvashi/23026b304a1f061a13a5868590ce9f70 to your computer and use it in GitHub Desktop.
URLルーティング実装
import http from "http";
import url from "url";
async function route(pathname: string, req: any, res: any) {
switch (pathname) {
// TODO: api の reverse proxy
case "/page1": {
page1Action(req, res);
break;
}
case "/page2": {
page2Action(req, res);
break;
}
default: {
// TODO: 静的に一致しない場合はコンテンツをそのまま返す実装を入れておく
break;
}
}
}
const server = createServer();
server.on("request", async (req, res) => {
const { pathname } = url.parse(req.url);
if (!pathname) throw new Error("Error! pathname is null.");
await route(pathname, req, res);
res.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment