rsnext/test/integration/custom-server/server.js
Joey Tepperman cdc7f01954
Add warning when a page is rendered without a starting / (#11418)
* Add error/warning when a page is rendered without a /

Throws an error for development and gives a warning in production

* Add tests for error when rendering without starting slash

* Update to always warn and add err.sh

* Update errors/render-no-starting-slash.md

Co-authored-by: JJ Kasper <jj@jjsweb.site>
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
2020-04-06 11:54:42 -05:00

57 lines
1.4 KiB
JavaScript

const http = require('http')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const dir = __dirname
const port = process.env.PORT || 3000
const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()
app.prepare().then(() => {
const server = new http.Server(async (req, res) => {
if (req.url === '/no-query') {
return app.render(req, res, '/no-query')
}
if (/setAssetPrefix/.test(req.url)) {
app.setAssetPrefix(`http://127.0.0.1:${port}`)
} else if (/setEmptyAssetPrefix/.test(req.url)) {
app.setAssetPrefix(null)
} else {
// This is to support multi-zones support in localhost
// and may be in staging deployments
app.setAssetPrefix('')
}
if (/test-index-hmr/.test(req.url)) {
return app.render(req, res, '/index')
}
if (/dashboard/.test(req.url)) {
return app.render(req, res, '/dashboard/index')
}
if (/static\/hello\.text/.test(req.url)) {
return app.render(req, res, '/static/hello.text')
}
if (/no-slash/.test(req.url)) {
try {
await app.render(req, res, 'dashboard')
} catch (err) {
res.end(err.message)
}
}
handleNextRequests(req, res)
})
server.listen(port, err => {
if (err) {
throw err
}
console.log(`> Ready on http://localhost:${port}`)
})
})