Created
October 2, 2018 16:46
-
-
Save pkellner/365d7031774ce11287b0aaac02a6e02b to your computer and use it in GitHub Desktop.
When using next-router with nextjs and you set useFileSystemPublicRoutes: false, you need a server.js like this to find your home route. See https://spectrum.chat/?t=eeaebd2e-e337-401c-9e29-d7558202e76b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createServer } = require("http"); | |
const next = require("next"); | |
const routes = require("./routes"); | |
const { parse } = require("url"); | |
const port = parseInt(process.env.PORT, 10) || 3000; | |
const dev = process.env.NODE_ENV !== "production"; | |
const app = next({ dev }); | |
const handler = routes.getRequestHandler(app); | |
app.prepare().then(() => { | |
createServer((req, res) => { | |
const parsedUrl = parse(req.url, true); | |
const { pathname, query } = parsedUrl; | |
if (pathname === "/") { | |
app.render(req, res, "/", query); | |
} else { | |
handler(req, res, parsedUrl); | |
} | |
}).listen(port, err => { | |
if (err) throw err; | |
console.log(`> Ready on http://localhost:${port}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment