вот ошибка:
listening on port 3000
node:_http_outgoing:573
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:573:11)
at ServerResponse.header (C:\OpenServer\domains\mysite.local\node_modules\e xpress\lib\response.js:771:10)
at ServerResponse.send (C:\OpenServer\domains\mysite.local\node_modules\e xpress\lib\response.js:170:12)
at C:\OpenServer\domains\mysite.local\app.js:160:13
at C:\OpenServer\domains\mysite.local\node_modules\ex press-fileupload\lib\utilities.js:56:57
at WriteStream.<anonymous> (C:\OpenServer\domains\mysite.local\node_modules\e xpress-fileupload\lib\utilities.js:181:29)
at WriteStream.emit (node:events:369:20)
at emitCloseNT (node:internal/streams/destroy:169:10)
at emitErrorCloseNT (node:internal/streams/destroy:154:3) {
code: 'ERR_HTTP_HEADERS_SENT'
}
вот app
[JS]var express = require('express');
var mysql = require('mysql');
const fileUpload = require('express-fileupload');
var app = express();
const bodyParser = require("body-parser");
cookies = require('cookie-parser');
app.use(cookies());
app.use(fileUpload());
///
/// Create connection to MySQL database server.
///
function getMySQLConnection() {
return mysql.createConnection({
host : 'localhost',
user : 'mysql',
password : 'mysql',
database : 'elektronika'
});
}
const urlencodedParser = bodyParser.urlencoded({extended: false});
app.set('view engine', 'ejs');
///
app.use('/input', express.static(__dirname + '/files/sphoto/'));
app.use('/smart', express.static(__dirname + '/files/sphoto/'));
app.get('/addtocookie/:param', function(req, res){
if(req.params.param=='vprodaj'){
if(req.cookies.vprodaj=''){
res.cookie('vprodaj', 'checked');
res.send();
}else{
res.cookies('vprodaj', '');
res.send();
}
}else if(req.params.param=='soskidkoi'){
if(req.cookies.soskidkoi=''){
res.cookies('soskidkoi', 'checked');
res.send();
}else{
res.cookise('soskidkoi', '');
res.send();
}
}else if(req.params.param=='dbesplat'){
if(req.cookies.dbesplat=''){
res.cookies('dbesplat', 'checked');
res.send();
}else{
res.cookies('dbesplat', '');
res.send();
}
}else if(req.params.param=='promokodi'){
if(req.cookies.promokodi=''){
res.cookie('promokodi', 'checked');
res.send();
}else{
res.cookies('promokodi', '');
res.send();
}
}else if(req.params.param=='presents'){
if(req.cookies.presents=''){
res.cookies('presents', 'checked');
res.send();
}else{
res.cookies('presents', '');
res.send();
}
}else if(req.params.param=='newtov'){
if(req.cookies.newtov=''){
res.cookies('newtov', 'checked');
res.send();
}else{
res.cookies('newtov', '');
res.send();
}
}else if(req.params.param=='uctov'){
if(req.cookies.uctov=''){
res.cookies('uctov', 'checked');
res.send();
}else{
res.cookies('uctov', '');
res.send();
}
}else if(req.params.param=='garantprod'){
if(req.cookies.garantprod=''){
res.cookies('garantprod', 'checked');
res.send();
}else{
res.cookies('garantprod', '');
res.send();
}
}else if(req.params.param=='checked'){
if(req.cookies.checked=''){
res.cookies('vpcheckedrodaj', 'checked');
res.send();
}else{
res.cookies('checked', '');
res.send();
}
}else if(req.params.param=='oswin'){
if(req.cookies.oswin=''){
res.cookies('oswin', 'checked');
res.send();
}else{
res.cookies('oswin', '');
res.send();
}
}else if(req.params.param=='osios'){
if(req.cookies.osios=''){
res.cookies('osios', 'checked');
res.send();
}else{
res.cookies('osios', '');
res.send();
}
}else if(req.params.param=='osandrod'){
if(req.cookies.osandrod=''){
res.cookies('osandrod', 'checked');
res.send();
}else{
res.cookies('osandrod', '');
res.send();
}
}else if(req.params.param=='side'){
if((req.cookies.side=='') || (req.cookies.side=='DESK')){
res.cookies('side', 'ASK');
res.send();
}else{
res.cookies('side', 'DESK');
res.send();
}
}
});
/// To get collection of person saved in MySQL database.
///
///////////////////////inp//////////
app.get("/input", urlencodedParser, function (request, response) {
response.render('input');
});
app.post("/input", urlencodedParser, function (request, response) {
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var randomIndex = Math.floor(Math.random() * alphabet.length);
if (!request.files || Object.keys(request.files).length === 0) {
return response.status(400).send('No files were uploaded.');
}
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
let sampleFile = request.files.photo;
var photon=__dirname+'/files/sphoto/'+randomIndex+request.files.photo.name;
// Use the mv() method to place the file somewhere on your server
sampleFile.mv((__dirname+'/files/sphoto/' + photon), function(err) {
if (err)
return response.status(500).send(err);
response.send('File uploaded!');
});
////////////////////////////////////
if(!request.body) return response.sendStatus(400);
const bodyParser = require("body-parser");
var connection = getMySQLConnection();
connection.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO `smartphone` (`id`,`prod`,`name`,`sim`,`razresh`,`acum`,`proc`,`oper`,`sd`,`cam`,`gps`,`description`,`giroscope`,`payment`,`photo`,`price`,`sale`,`dbesplat`,`promokodi`, `presents`,`garantprod`) VALUES (?)";
var values = ['NULL',request.body.prod,request.body.name,request.body.sim,request.body.display,request.body.acum,request.body.proc,request.body.oper,request.body.sd,request.body.cam,request.body.gps,request.body.description,request.body.giroscope,request.body.payment,photon,request.body.price,request.body.sale,request.body.dbesplat,request.body.promokodi, request.body.presents,request.body.garantprods];
connection.query(sql, [values], function (err, result) {
if (err){
throw err;
}
});
});
});app.listen(3000, function () {
console.log('listening on port', 3000);
});[/JS]