我正在做一個簡單的Rest服務,它必須向數據庫(MongoDB)中添加JSON請求中的數據。當我提出到服務請求的問題正在發生,該服務器發生故障,出現以下錯誤:NodeJS發送響應時出錯
[email protected] start C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller node ./bin/www
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version POST /controller/addagent 200 25.762 ms - 0 _http_outgoing.js:335 throw new Error('Can\'t set headers after they are sent.'); ^ Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11) at ServerResponse.header (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\express\lib\response.js:718:10) at ServerResponse.send (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\express\lib\response.js:163:12) at Promise. (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\routes\controller.js:26:15) at Promise. (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\monk\node_modules\mpromise\lib\promise.js:178:8) at Promise.emit (events.js:129:20) at Promise.emit (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\monk\node_modules\mpromise\lib\promise.js:84:38) at Promise.fulfill (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\monk\node_modules\mpromise\lib\promise.js:97:20) at Promise.resolve (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\monk\node_modules\mpromise\lib\promise.js:126:15) at Immediate._onImmediate (C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\node_modules\monk\lib\collection.js:343:23)
npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\nodejs\\node.exe" "C:\nodejs\node_modules\npm\bin\npm-cli.js" "start" npm ERR! node v0.12.7 npm ERR! npm v2.11.3 npm ERR! code ELIFECYCLE npm ERR! [email protected] start:
node ./bin/www
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] start script 'node ./bin/www'. npm ERR! This is most likely a problem with the controller package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node ./bin/www npm ERR! You can get their info via: npm ERR! npm owner ls controller npm ERR! There is likely additional logging output above.npm ERR! Please include the following file with any support request: npm ERR! C:\Users\Fernando\Google Drive\TCC_Fernando_Felipe\Controller\controller\npm-debug.log
在服務器端的代碼是:
controller.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a controller');
});
/*
* POST to adduser.
*/
**router.post('/addagent', function(req, res) {
var db = req.db;
var collection = db.get('controller');
var agents = req.body.agents;
if(agents.length && agents.length >0)
{
for(var i=0; i < agents.length; ++i){
var cAgent = agents[i];
collection.insert(cAgent, function(err, result){
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
});
}
}
});**
module.exports = router;
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//Added for database connection
var mongo=require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/controller');
var routes = require('./routes/index');
var users = require('./routes/users');
var controller = require('./routes/controller')
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Make our db accessible to our router
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/users', users);
app.use('/controller',controller);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
該錯誤似乎是由下面的行塊引起的。我不知道爲什麼,因爲res變量只能在那個時候訪問。
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
如果問題解決了,您應該接受您自己的答案並關閉問題 –