Created
November 24, 2020 14:47
-
-
Save hagevvashi/23026b304a1f061a13a5868590ce9f70 to your computer and use it in GitHub Desktop.
URLルーティング実装
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"; | |
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