已经回答了类似的问题,但是我还是无法解决自己的错误。
它是使用Express.js的服务器端应用程序。加载主页(http://localhost:3000/index.html)后,每刷新一次〜5次,就会出现错误:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\express\lib\response.js:767:10)
at ServerResponse.contentType (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\express\lib\response.js:595:15)
at ServerResponse.send (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\express\lib\response.js:145:14)
at done (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\express\lib\response.js:1004:10)
at Object.exports.renderFile (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\jade\lib\index.js:417:11)
at View.render (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\School\Desktop\Web Technologie\Express\mokershop\node_modules\express\lib\application.js:640:10)
所有代码:
var http = require('http');
var url = require('url');
var createError = require('http-errors');
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var serveStatic = require('serve-static');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var SQLiteStone = require('connect-sqlite3')(session);
var fs = require('fs');
var app = express();
// http logger
var logger = require('morgan');
var accessLogStream = fs.createWriteStream('access.log', { flags: 'a' })
app.use(logger('combined', { stream: accessLogStream }))
// importing db CHECK
var file = 'test.db';
var exists = fs.existsSync(file);
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
// session initialisation
var FileStore = require('session-file-store')(session);
app.use(session({
name: 'server-session-cookie-id',
secret: 'XXXXXXXX',
saveUninitialized: true,
resave: true,
store: new FileStore()
}));
// making virtual path prefix for static folder CHECK
var staticPath = path.join(__dirname, "static");
app.use(express.static(staticPath));
var publicPath = path.join(__dirname, "public");
app.use(express.static(publicPath));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
如果有人能指出问题出在哪里,我将不胜感激。