Skip to content

Instantly share code, notes, and snippets.

@friedjoff
Created January 4, 2019 13:22
Show Gist options
  • Save friedjoff/7171860fd38dcbb68f8bfc5139ab45a3 to your computer and use it in GitHub Desktop.
Save friedjoff/7171860fd38dcbb68f8bfc5139ab45a3 to your computer and use it in GitHub Desktop.
Next.js routing example
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