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

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

This commit is contained in:
Sergio Xalambrí 2018-01-31 03:22:43 -05:00 committed by Tim Neutkens
parent 8a4a9a10c2
commit 99272808d9
2 changed files with 20 additions and 17 deletions

View file

@ -1,17 +1,18 @@
{
"main": "server.js",
"scripts": {
"dev": "node server.js",
"dev": "micro-dev --watch server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
"start": "NODE_ENV=production micro"
},
"dependencies": {
"micro": "^8.0.1",
"micro": "^9.1.0",
"micro-route": "^2.4.0",
"next": "latest",
"react": "^16.1.1",
"react-dom": "^16.1.1"
},
"devDependencies": {
"micro-dev": "^1.1.2"
"micro-dev": "^2.2.0"
}
}

View file

@ -1,29 +1,31 @@
const micro = require('micro')
const match = require('micro-route/match')
const { parse } = require('url')
const match = require('micro-route/match')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
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 { query } = parsedUrl
if (match(req, '/a')) {
if (isA(req)) {
return app.render(req, res, '/b', query)
} else if (match(req, '/b')) {
} else if (isB(req)) {
return app.render(req, res, '/a', query)
}
return handle(req, res, parsedUrl)
})
}
app.prepare().then(() => {
server.listen(port, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
async function setup (handler) {
await app.prepare()
return handler
}
module.exports = setup(main)