I would like to:
- Retrieve data entered from input forms.
- send data through socket.io and send console log done
- get data on express app.js
- paste the data into the mongodb database using mongoose.
I understand that it is publishing messages, but not sure how to send data. thank you in advance
my app.js is configured as follows:
//app.js config ... var socket = io.listen(server); socket.on('connection', function(socket) { console.log('socket.io connected'); }); app.post('/go', function(req, res) { socket.on('data', function(data) { new Order({ routeFrom : data.routeFrom, routeTo : data.routeTo, leaving: data.leaving }).save(function(err, docs) { if(err) { console.log("error"); } res.json(data); }); socket.emit('callback', {done: 'Done'}); }); });
my index.ejs file:
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://localhost:3000'); $('#send').on('click', function() { $.post('/go', { socket.emit('data', { routeFrom: $('#rf').val(), routeTo: $('#rt').val(), leaving: $('#l').val() }); }); socket.on('callback', function(data) { console.log(data); }); });
source share