1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/custom-server-micro/server.js

32 lines
683 B
JavaScript
Raw Normal View History

const { parse } = require('url')
const match = require('micro-route/match')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const isA = req => match(req, '/a')
const isB = req => match(req, '/b')
async function main (req, res) {
const parsedUrl = parse(req.url, true)
const { query } = parsedUrl
if (isA(req)) {
return app.render(req, res, '/b', query)
} else if (isB(req)) {
return app.render(req, res, '/a', query)
}
return handle(req, res, parsedUrl)
}
async function setup (handler) {
await app.prepare()
return handler
}
module.exports = setup(main)