app.js
var app = require('./config/express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.set('io',io);
var porta = process.env.PORT || 3000;
var server = http.listen(porta, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
index.ejs
<script src="/socket.io/socket.io.js" charset="utf-8"></script>
<script>
var socket = io();
socket.on('novaPromocao', function (data) {
//document.location.href= "/produtos/"+data.livro.id+"?promocao="+data.mensagem;
alert(data.livro);
});
</script>
promocoes.js
module.exports = function(app){
app.get('/promocoes/form', function(req, res){
var connection = app.infra.connectionFactory();
var listaPromocoes = new app.infra.produtosDAO(connection);
listaPromocoes.lista (function(errs, result){
res.render('promocoes/form', {lista: result});
});
});
app.post('/promocoes', function(req, res){
var promocoes = req.body;
app.get('io').emit('novaPromocao', promocoes);
res.redirect('promocoes/form');
})
}