mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
3d94ae0a7d
As prepare is only needed to boot up the hot reloader + exportPathMap routes in development, it's not longer a requirement in the production server.
28 lines
543 B
JavaScript
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)
|
|
}
|
|
}
|
|
}
|
|
}
|