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/server/router.js
2018-10-02 00:55:31 +02:00

28 lines
543 B
JavaScript

import pathMatch from './lib/path-match'
export const route = pathMatch()
export default class Router {
constructor (routes = []) {
this.routes = routes
}
add (route) {
this.routes.unshift(route)
}
match (req, res, parsedUrl) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
return
}
const { pathname } = parsedUrl
for (const route of this.routes) {
const params = route.match(pathname)
if (params) {
return () => route.fn(req, res, params, parsedUrl)
}
}
}
}