In Node.JS espresso, significa “res.render” fine della richiesta http?
-
26-10-2019 - |
Domanda
Quindi, solo fare "res.render" quando si è certi che tutto è finito, giusto? Perché finisce la richiesta e spara fuori una pagina web.
Soluzione
Se non si fornisce un callback per res.render(view[, options[, fn]])
sarà automaticamente dare una risposta con stato 200 HTTP e Content-Type: text / html
res.render('view', {}, function() {
while (true); // should block
});
res.render (vista [, opzioni [, fn]])
Render vista con le opzioni date e opzionale callback Fn. Quando una funzione callback viene data una risposta non verrà effettuato automaticamente, ma altrimenti una risposta di 200 e testo / html è dato.
Altri suggerimenti
Con il github master attuale commettere , questo è res.render
in lib / view.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);
}
}
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow