我有一个非常奇怪的问题,我会尝试解释它: 我向服务器发送一个POST请求,然后将数据保存在数据库中,我将这些函数导出到一个单独的文件中。我怎样才能发出通过socket保存的内容,因为我的./database.js中不能要求('socket.io')?
这是一个片段:
var app = require('express')();
var database = require('./database');
var serv = require('http').createServer(app);
var socket = require('socket.io')(serv);
....
app.post('/test', function(req, res){
database.save(model, req.body, res); //it saves the body to mongo and it works
*edit//If I paste the code of database.save() here as is I can invoke emit
//Here I want to emit the same data too
//If I use the following I am receiving Error: Can't set headers after they are sent.
socket.emit('value', JSON.stringify(req.body));
};