1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/router.js
Arunoda Susiripala abe6e3ea47 Use a latest version of path-to-regexp for path-match (#3655)
* Use the latest path-to-regexp for path-match.

* Fix the error route resolving.

* Give proper credit to path-match

* Remove an unwanted route.

* Revert "Remove an unwanted route."

This reverts commit 523c1687da8ddd499819e70df81567ec208e5998.

* Add a comment why we need to keep _error.js route.
2018-02-01 19:54:09 +01: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)
}
}
}
}
}