From 40738c6e44ffd901be0935bbf865ed19499d7ece Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 2 Mar 2017 00:30:07 +0100 Subject: [PATCH] Throw Error when url.parse without true is parsed (#1282) * Throw Error when url.parse without true is parsed This is a bit more descriptive when this mistake is made by the user. * Parse when needed * Parse querystring if it is not provided --- server/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/server/index.js b/server/index.js index 0aed4cab..27978aa6 100644 --- a/server/index.js +++ b/server/index.js @@ -1,5 +1,6 @@ import { resolve, join } from 'path' -import { parse } from 'url' +import { parse as parseUrl } from 'url' +import { parse as parseQs } from 'querystring' import fs from 'mz/fs' import http, { STATUS_CODES } from 'http' import { @@ -33,12 +34,14 @@ export default class Server { getRequestHandler () { return (req, res, parsedUrl) => { + // Parse url if parsedUrl not provided if (!parsedUrl) { - parsedUrl = parse(req.url, true) + parsedUrl = parseUrl(req.url, true) } - if (!parsedUrl.query) { - throw new Error('Please provide a parsed url to `handle` as third parameter. See https://github.com/zeit/next.js#custom-server-and-routing for an example.') + // Parse the querystring ourselves if the user doesn't handle querystring parsing + if (typeof parsedUrl.query === 'string') { + parsedUrl.query = parseQs(parsedUrl.query) } return this.run(req, res, parsedUrl) @@ -222,7 +225,7 @@ export default class Server { } } - async render404 (req, res, parsedUrl = parse(req.url, true)) { + async render404 (req, res, parsedUrl = parseUrl(req.url, true)) { const { pathname, query } = parsedUrl res.statusCode = 404 return this.renderError(null, req, res, pathname, query)