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

check if response is already sent (#569)

This commit is contained in:
Naoyuki Kanezawa 2016-12-31 04:55:20 +09:00 committed by Guillermo Rauch
parent 0f11018819
commit c210a2e73c

View file

@ -108,12 +108,16 @@ export async function renderErrorJSON (err, res, { dir = process.cwd(), dev = fa
} }
export function sendHTML (res, html) { export function sendHTML (res, html) {
if (res.finished) return
res.setHeader('Content-Type', 'text/html') res.setHeader('Content-Type', 'text/html')
res.setHeader('Content-Length', Buffer.byteLength(html)) res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(html) res.end(html)
} }
export function sendJSON (res, obj) { export function sendJSON (res, obj) {
if (res.finished) return
const json = JSON.stringify(obj) const json = JSON.stringify(obj)
res.setHeader('Content-Type', 'application/json') res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-Length', Buffer.byteLength(json)) res.setHeader('Content-Length', Buffer.byteLength(json))