Server.js
Código Javascript:
Ver original
var express = require('express'), app = module.exports = express(), url = require('url'), path = require('path'), mysql = require('mysql'); app.engine('.html', require('ejs').__express); app.set('views', __dirname + '/views'); app.set('view engine', 'html'); var SQLanco = function(host, user, password, database) { try { this.connection = mysql.createConnection({ host: host, user: user, password: password, database: database }) this.connection.connect(function(err) { if (!err) console.log('Connect SQL success full'); else console.log(err.message); }); return this.connection; }catch(err) { console.log(err.message); } } var ClientSQL = new SQLanco('localhost', 'root', '', 'db'); app.locals.numeroComentarios = function (id) { ClientSQL.query("SELECT * FROM app_mensajes WHERE ID_post = "+ id, function(err, result1) { return result1.length; }); }); app.get('/', function(req, res) { ClientSQL.query("SELECT * FROM app_post", function(err, data) { res.render('index', { post : data }); }); }); app.listen(80);
index.html
Código HTML:
Ver original
numeroComentarios() me retorna undefined :/