Boa tarde!
Segui os passos descritos, mas estou encontrando o seguinte erro:
TypeError: Cannot read property 'emit' of undefined
at C:\node\casadocodigo\app\routes\promocoes.js:15:22
at Layer.handle [as handle_request] (C:\node\casadocodigo\node_modules\express\lib\router\layer.js:95:5)
at next (C:\node\casadocodigo\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\node\casadocodigo\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\node\casadocodigo\node_modules\express\lib\router\layer.js:95:5)
at C:\node\casadocodigo\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\node\casadocodigo\node_modules\express\lib\router\index.js:335:12)
at next (C:\node\casadocodigo\node_modules\express\lib\router\index.js:275:10)
at C:\node\casadocodigo\node_modules\express-validator\lib\express_validator.js:250:5
at Layer.handle [as handle_request] (C:\node\casadocodigo\node_modules\express\lib\router\layer.js:95:5)
app/views/home/index.ejs
</footer>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
socket.on('novaPromocao', function(data){
alert('Livro em promoção ' + data.livro.id);
});
</script>
</body>
</html>
app/routes/promocoes.js
module.exports = function(app){
app.get('/promocoes', function(req, res){
var connection = app.infra.connectionFactory();
var produtosDAO = new app.infra.produtosDAO(connection);
produtosDAO.lista(function(err, results){
res.render('promocoes/form',{
lista:results
});
});
connection.end();
});
app.post('/promocoes',function(req, res){
var promocao = req.body;
app.get('io').emit('novaPromocao', promocao);
res.redirect('promocoes');
});
}
app.js
var app = require('./config/express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.set('io'.io);
app.listen(3000, function(){
console.log('Servidor rodando');
})
Obrigado.