1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/packages/next/server/router.js
Tim Neutkens b1c4f3aec4
Monorepo (#5341)
- Implements Lerna
- Moves all source code into `packages/next`
- Keeps integration tests in the root directory
2018-10-01 01:02:10 +02:00

31 lines
656 B
JavaScript

import pathMatch from './lib/path-match'
const route = pathMatch()
export default class Router {
constructor () {
this.routes = new Map()
}
add (method, path, fn) {
const routes = this.routes.get(method) || new Set()
routes.add({ match: route(path), fn })
this.routes.set(method, routes)
}
match (req, res, parsedUrl) {
const routes = this.routes.get(req.method)
if (!routes) return
const { pathname } = parsedUrl
for (const r of routes) {
const params = r.match(pathname)
if (params) {
return async () => {
return r.fn(req, res, params, parsedUrl)
}
}
}
}
}