Skip to content

Commit

Permalink
Allow parsed url to be passed down (#950)
Browse files Browse the repository at this point in the history
* Allow parsed url to be passed down

* Update example to reflect url passing

* Check if passed url.query is empty

* Rename url to parsedUrl
  • Loading branch information
timneutkens authored and arunoda committed Feb 2, 2017
1 parent 24edfbd commit 59281ad
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions examples/custom-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
22 changes: 13 additions & 9 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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])
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions server/router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parse } from 'url'
import pathMatch from 'path-match'

const route = pathMatch()
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 59281ad

Please sign in to comment.