Implementei o exercício proposto e conferi a resposta, no entanto, a aplicação não funciona. Recebo o erro abaixo no browser ao chamar a url http://localhost:3000/ :
TypeError: C:\Desenvolvimento\workspace\nodejs\casadocodigo\app\views\home\index.ejs:93
91| <li class="col-left">
92| <a href="linkDetalhe" class="block clearfix">
>> 93| <img width="170" height="240" src="urlImagem" alt="<%=livros[i].titulo%>" title="<%=livros[i].titulo%>"/>
94|
95| <h2 class="product-title"><%=livros[i].titulo%></h2>
96| <small class="buy-button">Lançamento!</small>
Cannot read property 'titulo' of undefined
at eval (eval at compile (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\ejs\lib\ejs.js:481:12), <anonymous>:14:32)
at returnedFn (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\ejs\lib\ejs.js:512:17)
at View.exports.renderFile [as engine] (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\ejs\lib\ejs.js:364:31)
at View.render (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\express\lib\view.js:126:8)
at tryRender (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\express\lib\application.js:639:10)
at Function.render (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\express\lib\response.js:960:7)
at Query._callback (C:\Desenvolvimento\workspace\nodejs\casadocodigo\app\routes\home.js:7:8)
at Query.Sequence.end (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
at Query._handleFinalResultPacket (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
at Query.EofPacket (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\protocol\sequences\Query.js:128:8)
at Protocol._parsePacket (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\protocol\Protocol.js:280:23)
at Parser.write (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\protocol\Parser.js:74:12)
at Protocol.write (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Desenvolvimento\workspace\nodejs\casadocodigo\node_modules\mysql\lib\Connection.js:109:28)
at emitOne (events.js:96:13)
home.js
module.exports = function(app){
app.get('/', function(req, res){
var connection = app.infra.connectionFactory();
var ProdutosDAO = new app.infra.ProdutosDAO(connection);
ProdutosDAO.lista(function(error, results,fileds){
res.render('home/index',{livros:results});
});
connection.end();
});
}
express.js
var express = require('express');
var load = require('express-load');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
module.exports = function (){
var app = express();
app.use(express.static('./app/public'));
app.set('view engine','ejs');
app.set('views','./app/views');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(expressValidator());
load('routes',{cwd:'app'})
.then('infra')
.into(app);
return app;
}