Created
January 4, 2019 13:22
-
-
Save friedjoff/7171860fd38dcbb68f8bfc5139ab45a3 to your computer and use it in GitHub Desktop.
Next.js routing example
This file contains 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 dbClient = require('./db') | |
const app = next() | |
const handle = app.getRequestHandler() | |
app.prepare().then(() => { | |
createServer((req, res) => { | |
// Fetch routing information from database. | |
const { page, id } = dbClient.getRoute(req.url) | |
// Render different pages based on dynamic routing information. | |
app.render(req, res, page, { id }) | |
}).listen(3000, err => { | |
if (err) throw err | |
console.log(`> Ready on http://localhost:3000`) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment