Repository URL to install this package:
|
Version:
0.0.15 ▾
|
/**
* @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 { logger } from '../log';
export const respondRedirect = (res, url) => {
res.status(302).setHeader('Location', url);
res.end();
};
// @@log need to use req log here
export const respondHyperText = (res, status, html) => {
const webpage = `<!DOCTYPE html>${html}`;
logger.debug('[SSR] =========> SENDING_SITE', html.length);
try {
res.status(status || 200);
res.setHeader('Cache-Control', 'public, max-age=900');
// moved this headers to nginx
// res.setHeader('Strict-Transport-Security', 'max-age=788400')
res.send(webpage);
res.end();
}
catch (responseException) {
logger.error('[EXCEPTION_ALERT] responseException', responseException);
}
};
export const fromExceptionToMarkupString = (exception) => {
const { stack, message } = exception;
const htmlStack = stack
.split('\n')
.map(line => `<br />` + line)
.join('\n');
let htmlError = `<!DOCTYPE html>`;
htmlError += `<h1 style="background-color: red">${message}</h1>\n`;
htmlError += `<pre style="font: monospace">${htmlStack}</pre>`;
return htmlError;
};
export const respondError = (res, exception) => {
// require('fliplog').data({ exception }).echo()
// .json({ errorMsg: exception.message, stack: exception.stack })
const htmlError = fromExceptionToMarkupString(exception);
res.status(500).send(htmlError);
res.end();
};
//# sourceMappingURL=responders.js.map