Repository URL to install this package:
|
Version:
1.2.12 ▾
|
/**
* @see https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching
* @see https://medium.com/walmartlabs/reactjs-ssr-profiling-and-caching-5d8e9e49240c
* @fileoverview small reusable express responders
*/
import { Response, Request } from './typings'
export const respondRedirect = (res: Response, url: string) => {
res.status(302).setHeader('Location', url)
res.end()
}
export const respondHyperText = (
res: Response,
status: number,
html: string
) => {
const webpage = `<!DOCTYPE html>${html}`
console.debug('[SSR] =========> SENDING_SITE', html.length)
try {
res.status(status || 200)
res.setHeader('Cache-Control', 'public, max-age=600')
res.setHeader('Strict-Transport-Security', 'max-age=788400')
res.send(webpage)
res.end()
} catch (responseException) {
console.error('[EXCEPTION_ALERT] responseException', responseException)
}
}
export const respondError = (res: Response, exception: Error) => {
// require('fliplog').data({ exception }).echo()
// .json({ errorMsg: exception.message, stack: exception.stack })
const { stack, message } = exception
const htmlStack = stack
.split('\n')
.map(line => {
return `<br />` + line
})
.join('\n')
let htmlError = `<!DOCTYPE html>`
htmlError += `<h1 style="background-color: red">${message}</h1>\n`
htmlError += `<pre style="font: monospace">${htmlStack}</pre>`
res.status(500).send(htmlError)
res.end()
}