mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Allow parsed url to be passed down (#950)
* Allow parsed url to be passed down * Update example to reflect url passing * Check if passed url.query is empty * Rename url to parsedUrl
This commit is contained in:
parent
24edfbdea7
commit
59281adef3
|
@ -382,14 +382,15 @@ const handle = app.getRequestHandler()
|
|||
|
||||
app.prepare().then(() => {
|
||||
createServer((req, res) => {
|
||||
const { pathname, query } = parse(req.url, true)
|
||||
const parsedUrl = parse(req.url, true)
|
||||
const { pathname, query } = parsedUrl
|
||||
|
||||
if (pathname === '/a') {
|
||||
app.render(req, res, '/b', query)
|
||||
} else if (pathname === '/b') {
|
||||
app.render(req, res, '/a', query)
|
||||
} else {
|
||||
handle(req, res)
|
||||
handle(req, res, parsedUrl)
|
||||
}
|
||||
})
|
||||
.listen(3000, (err) => {
|
||||
|
|
|
@ -9,14 +9,15 @@ const handle = app.getRequestHandler()
|
|||
app.prepare()
|
||||
.then(() => {
|
||||
createServer((req, res) => {
|
||||
const { pathname, query } = parse(req.url, true)
|
||||
const parsedUrl = parse(req.url, true)
|
||||
const { pathname, query } = parsedUrl
|
||||
|
||||
if (pathname === '/a') {
|
||||
app.render(req, res, '/b', query)
|
||||
} else if (pathname === '/b') {
|
||||
app.render(req, res, '/a', query)
|
||||
} else {
|
||||
handle(req, res)
|
||||
handle(req, res, parsedUrl)
|
||||
}
|
||||
})
|
||||
.listen(3000, (err) => {
|
||||
|
|
|
@ -33,8 +33,12 @@ export default class Server {
|
|||
}
|
||||
|
||||
getRequestHandler () {
|
||||
return (req, res) => {
|
||||
this.run(req, res)
|
||||
return (req, res, parsedUrl) => {
|
||||
if (!parsedUrl || parsedUrl.query === null) {
|
||||
parsedUrl = parse(req.url, true)
|
||||
}
|
||||
|
||||
this.run(req, res, parsedUrl)
|
||||
.catch((err) => {
|
||||
if (!this.quiet) console.error(err)
|
||||
res.statusCode = 500
|
||||
|
@ -102,8 +106,8 @@ export default class Server {
|
|||
await this.serveStatic(req, res, p)
|
||||
},
|
||||
|
||||
'/:path*': async (req, res) => {
|
||||
const { pathname, query } = parse(req.url, true)
|
||||
'/:path*': async (req, res, params, parsedUrl) => {
|
||||
const { pathname, query } = parsedUrl
|
||||
await this.render(req, res, pathname, query)
|
||||
}
|
||||
}
|
||||
|
@ -126,19 +130,19 @@ export default class Server {
|
|||
})
|
||||
}
|
||||
|
||||
async run (req, res) {
|
||||
async run (req, res, parsedUrl) {
|
||||
if (this.hotReloader) {
|
||||
await this.hotReloader.run(req, res)
|
||||
}
|
||||
|
||||
const fn = this.router.match(req, res)
|
||||
const fn = this.router.match(req, res, parsedUrl)
|
||||
if (fn) {
|
||||
await fn()
|
||||
return
|
||||
}
|
||||
|
||||
if (req.method === 'GET' || req.method === 'HEAD') {
|
||||
await this.render404(req, res)
|
||||
await this.render404(req, res, parsedUrl)
|
||||
} else {
|
||||
res.statusCode = 501
|
||||
res.end(STATUS_CODES[501])
|
||||
|
@ -203,8 +207,8 @@ export default class Server {
|
|||
}
|
||||
}
|
||||
|
||||
async render404 (req, res) {
|
||||
const { pathname, query } = parse(req.url, true)
|
||||
async render404 (req, res, parsedUrl = parse(req.url, true)) {
|
||||
const { pathname, query } = parsedUrl
|
||||
res.statusCode = 404
|
||||
this.renderError(null, req, res, pathname, query)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { parse } from 'url'
|
||||
import pathMatch from 'path-match'
|
||||
|
||||
const route = pathMatch()
|
||||
|
@ -14,16 +13,16 @@ export default class Router {
|
|||
this.routes.set(method, routes)
|
||||
}
|
||||
|
||||
match (req, res) {
|
||||
match (req, res, parsedUrl) {
|
||||
const routes = this.routes.get(req.method)
|
||||
if (!routes) return
|
||||
|
||||
const { pathname } = parse(req.url)
|
||||
const { pathname } = parsedUrl
|
||||
for (const r of routes) {
|
||||
const params = r.match(pathname)
|
||||
if (params) {
|
||||
return async () => {
|
||||
return r.fn(req, res, params)
|
||||
return r.fn(req, res, params, parsedUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue