继续在我的node.js服务器上收到以下警告

时间:2016-10-13 08:43:25

标签: javascript node.js

我的控制台继续发出以下警告。这可以来自app.js或任何其他路径文件:

由于我的缓存处理,错误似乎即将到来。我会在用户注销后处理缓存。

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at ServerResponse.header (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/response.js:163:12)
    at done (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/response.js:957:10)
    at View.exports.renderFile [as engine] (/home/ved/Dropbox/JAVA/eBay/test/node_modules/ejs/lib/ejs.js:363:10)
    at View.render (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/view.js:126:8)
    at tryRender (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/application.js:639:10)
    at EventEmitter.render (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/application.js:591:3)
    at ServerResponse.render (/home/ved/Dropbox/JAVA/eBay/test/node_modules/express/lib/response.js:961:7)
    at /home/ved/Dropbox/JAVA/eBay/test/app.js:159:7
GET /fonts/glyphicons-halflings-regular.woff 404 1.673 ms - 968
GET /fonts/glyphicons-halflings-regular.ttf 404 1.738 ms - 968
POST /viewProductPage - - ms - -

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');


//DEFINING EXTERNAL DEPENDENCY TO ENABLE SESSIONS IN THE APPLICATION
var session = require('client-sessions');


//DEFINING THE BASE ROUTE
var routes = require('./routes/index');

//DEFINING THE ROUTE TO GET THE SYSTEM USERS
var users  = require('./routes/users');


//DEFINING THE ROUTE TO ACCESS sell.js
//sell.js HANDLES ALL THE ITEMS POSTED
//INTERACTS WITH THE DATABASE FOR THE SAME
var sell = require('./routes/sell');


//ROUTES TO getUserData.js
//DEFINED AS A BASE ROUTE TO ACCESS SESSION FOR ANY USE
var getUserData = require('./routes/getUserData');





var productsRetriever = require('./routes/productsRetriever');


//DEFINING THE DEPENDENCY TO ACCESS LOGIN CONTROLS FOR THE SYSTEM
//USER LOGIN AND MOST IMPORTANTLY ASSIGNING A SESSION TO THAT USER IS HANDLED BY login.js
//getUserData.js RETRIEVES THE SESSION OF A USER INITIATED IN login.js
var login    = require('./routes/login');





var productDetail    = require('./routes/productDetail');



//DEFINING THE DEPENDENCY TO ACCESS register.js
//register.js HANDLES ALL THE USER REGISTRATION PATHS AS WELL AS FUNCTIONS
var register = require('./routes/register');



//INITIATING AN INSTANCE OF express js IN OUR APPLICATION
//ASSIGNING IT TO A VARIABLE APP
var app      = express();



// all environments
//configure the sessions with our application
app.use(session({
    cookieName    : 'session',
    secret        : 'cmpe273_ebay_app_fall_2016',
    duration      : 30 * 60 * 1000, //setting the time for active session
    activeDuration: 5 * 60 * 1000 // setting time for the session to be active when the window is open // 5 minutes set currently
})); 





// view engine setup
//SETS BASE PATH FOR ALL THE FILES IN THE VIEW
app.set('views', path.join(__dirname, 'views'));
//ASSIGNING THE ejs ENGINE TO ALL THE FILES WITHIN VIEWS DIRECTORY
app.set('view engine', 'ejs');

// 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')));


//GETS THE PATH WHEN A USER FIRST VISITS OUR APPLICATION
app.get('/', routes);


app.get('/users', users);

app.get('/login', login.login);
app.post('/checklogin', login.checklogin);
app.get('/getAllUsers', login.getAllUsers);


app.get('/sell', sell.sell);
app.post('/addSellItem', sell.addSellItem);


//app.post('/checklogin', login.checklogin);
app.get('/homepage',login.redirectToHomepage);





app.post('/logout',login.logout);



app.post('/getUserInfo', getUserData.getUserData);

app.get('/viewProduct',productDetail.getProductPage);

app.post('/productsRetriever', productsRetriever.getProducts);


app.post('/viewProductPage', productDetail.getProductPageDetails);

app.get('/register', register.register);
app.post('/registerdone', register.registerdone);
app.get('/getAllRegisteredUsers', register.getAllRegisteredUsers);





// 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;

1 个答案:

答案 0 :(得分:0)

这也发生在我身上。

问题是(对我来说)是我在我的路线中做了一些操作并且正在向用户发送响应但是在发生错误之后它由错误处理中间件处理(app.js的底部)它正在发送错误响应。

但是由于请求已在您的API路由中早些时候响应,因此日志中会显示此错误消息。尝试找出错误是什么。

尝试将console.log()放入错误处理程序中以查看跟踪。