En Node.js Express, ¿"Res.Render" finaliza la solicitud HTTP?
-
26-10-2019 - |
Pregunta
Entonces, solo "res.render" cuando estás seguro de que todo ha terminado, ¿verdad? Porque termina la solicitud y dispara una página web.
Solución
Si no proporciona una devolución de llamada a res.render(view[, options[, fn]])
Dará automáticamente una respuesta con 200 HTTP de estado y tipo de contenido: Texto/HTML
res.render('view', {}, function() {
while (true); // should block
});
res.render (ver [, opciones [, fn]])
Renderiza la vista con las opciones dadas y la devolución de llamada opcional fn. Cuando se da una función de devolución de llamada, no se realizará una respuesta automáticamente, sin embargo, de lo contrario, se da una respuesta de 200 y texto/HTML.
Otros consejos
Con la corriente Github Master Commit, esto es res.render
en lib/ver.js:
/**
* Render `view` with the given `options` and optional callback `fn`.
* When a callback function is given a response will _not_ be made
* automatically, however otherwise a response of _200_ and _text/html_ is given.
*
* Options:
*
* - `scope` Template evaluation context (the value of `this`)
* - `debug` Output debugging information
* - `status` Response status code
*
* @param {String} view
* @param {Object|Function} options or callback function
* @param {Function} fn
* @api public
*/
res.render = function(view, opts, fn, parent, sub){
// support callback function as second arg
if ('function' == typeof opts) {
fn = opts, opts = null;
}
try {
return this._render(view, opts, fn, parent, sub);
} catch (err) {
// callback given
if (fn) {
fn(err);
// unwind to root call to prevent
// several next(err) calls
} else if (sub) {
throw err;
// root template, next(err)
} else {
this.req.next(err);
}
}
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow