Skip to content

Instantly share code, notes, and snippets.

@pkellner
Created October 2, 2018 16:46
Show Gist options
  • Save pkellner/365d7031774ce11287b0aaac02a6e02b to your computer and use it in GitHub Desktop.
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
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