Commit 99272808 authored by Sergio Xalambrí's avatar Sergio Xalambrí Committed by Tim Neutkens

Update custom-server-micro example to latest version (#3594)

parent 8a4a9a10
{ {
"main": "server.js",
"scripts": { "scripts": {
"dev": "node server.js", "dev": "micro-dev --watch server.js",
"build": "next build", "build": "next build",
"start": "NODE_ENV=production node server.js" "start": "NODE_ENV=production micro"
}, },
"dependencies": { "dependencies": {
"micro": "^8.0.1", "micro": "^9.1.0",
"micro-route": "^2.4.0", "micro-route": "^2.4.0",
"next": "latest", "next": "latest",
"react": "^16.1.1", "react": "^16.1.1",
"react-dom": "^16.1.1" "react-dom": "^16.1.1"
}, },
"devDependencies": { "devDependencies": {
"micro-dev": "^1.1.2" "micro-dev": "^2.2.0"
} }
} }
const micro = require('micro')
const match = require('micro-route/match')
const { parse } = require('url') const { parse } = require('url')
const match = require('micro-route/match')
const next = require('next') const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production' const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev }) const app = next({ dev })
const handle = app.getRequestHandler() const handle = app.getRequestHandler()
const server = micro(async (req, res) => { const isA = req => match(req, '/a')
const isB = req => match(req, '/b')
async function main (req, res) {
const parsedUrl = parse(req.url, true) const parsedUrl = parse(req.url, true)
const { query } = parsedUrl const { query } = parsedUrl
if (match(req, '/a')) { if (isA(req)) {
return app.render(req, res, '/b', query) return app.render(req, res, '/b', query)
} else if (match(req, '/b')) { } else if (isB(req)) {
return app.render(req, res, '/a', query) return app.render(req, res, '/a', query)
} }
return handle(req, res, parsedUrl) return handle(req, res, parsedUrl)
}) }
app.prepare().then(() => { async function setup (handler) {
server.listen(port, err => { await app.prepare()
if (err) throw err return handler
console.log(`> Ready on http://localhost:${port}`) }
})
}) module.exports = setup(main)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment