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

28 lines
543 B
JavaScript
Raw Normal View History

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