I am trying to customize my server using Next.js:
const express = require('express')
const next = require('next')
port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({dev, dir: '../'})
const handle = app.getRequestHandler()
app.prepare().then(() => {
const server = express()
server.get('/a', (req, res) => {
res.send("Palin text: Hello From Express server Riko :)")
})
server.get('/b', (req, res) => {
return app.render(req, res, '/b', req.query)
})
server.get('/posts/:id', (req, res) => {
return app.render(req, res, '/posts', { id: req.params.id })
})
server.get('*', (req, res) => {
res.removeHeader('Transfer-Encoding')
res.setHeader('Connection', 'close')
return handle(req, res)
})
server.listen(port, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
When a page is being loaded i see that the response header Connection
is set to 'keep-alive'. And this results in very long response time.
I tried to change the header here:
server.get('*', (req, res) => {
res.removeHeader('Transfer-Encoding')
res.setHeader('Connection', 'close')
return handle(req, res)
})
Without success though.
This is the Next.js configuration file:
module.exports = {
distDir: 'build',
poweredByHeader: false
}
use this:
res.set('Connection', 'close');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With