问题
I am getting an "res.render is not a function error" in the Windows 10 Command Prompt when I try to run my node.js code.
What is causing this error and how can I get rid of it?
Here is my .js file:
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// HTTP request - duas alternativas
var http = require('http');
var request = require('request');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
//chama o express, que abre o servidor
var express = require('express');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
app.get('/home1', function(res){
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var json = JSON.parse(body);
var cotacao = json["bovespa"]["cotacao"];
var CotacaoDolar= json["dolar"]["cotacao"];
var VariacaoDolar=json["dolar"]["variacao"];
var CotacaoEuro=json["euro"]["cotacao"];
var VariacaoEuro=json["euro"]["variacao"];
var Atualizacao=json["atualizacao"];
res.render('cotacao.jade',{title:'Hey', message:'Sua cotação foi de'});
});
});
});
Error:
回答1:
I don't see where jade is hooked into your Express app. In order for res.render()
to know about jade templates, you have to hook a jade handler into Express.
If jade is installed properly, you should be able to do this to hook it into Express:
app.set('view engine', 'jade');
Express doc for this.
In addition, you have named two arguments in the same scope as res
. Change the name of the second one that is part of the http.get()
so you can still access the higher scoped one that is the actual Express response object.
app.get('/home1', function (res) {
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function (res2) {
var body = '';
res2.on('data', function (chunk) {
body += chunk;
});
res2.on('end', function () {
var json = JSON.parse(body);
var cotacao = json["bovespa"]["cotacao"];
var CotacaoDolar = json["dolar"]["cotacao"];
var VariacaoDolar = json["dolar"]["variacao"];
var CotacaoEuro = json["euro"]["cotacao"];
var VariacaoEuro = json["euro"]["variacao"];
var Atualizacao = json["atualizacao"];
res.render('cotacao.jade', {
title: 'Hey',
message: 'Sua cotação foi de'
});
});
});
});
回答2:
You're dealing with two sets of HTTP requests/responses.
One you are receiving:
app.get('/home1', function(res){
and one you are making:
http.get('http://developers.agenciaideias.com.br/cotacoes/json', function(res){
… but you've given them both the same variable name (res
) so one masks the other and prevents you from accessing it.
Change the name of one of them.
来源:https://stackoverflow.com/questions/34109203/res-render-is-not-a-function-error-in-node-js