我对Node.js很新,我遇到了一些问题。
当我尝试打开127.0.0.1:3000/bejelentkezes时收到以下错误:
POST /bejelentkezes 500 550.784 ms - 2259
GET /css/404.css 304 2.751 ms - -
1
sikeres bejelentkezes
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:170:12)
at done (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:967:10)
at Object.exports.renderFile (C:\Users\denes\Documents\e_wallet\node_modules\pug\lib\index.js:412:12)
at View.exports.__express [as engine] (C:\Users\denes\Documents\e_wallet\node_modules\pug\lib\index.js:455:11)
at View.render (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\view.js:128:8)
at tryRender (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:971:7)
这是我的代码:
var express = require('express');
var router = express.Router();
var tools = require('./adatbazis_v2');
router.get('/', function(req, res, next) {
res.render('login');
});
router.post('/', function(req,res,next) {
if (!req.body.email || !req.body.password) {
console.log('valami nincs kitoltve');
} else {
tools.checkUser(req.body.email, req.body.password, function(callback) {
if(callback == 1){
console.log("sikeres bejelentkezes");
res.render('index');
} else {
console.log('sikertelen bejelentkezes');
res.render('login');
}
});
}
});
module.exports = router;
这是功能:
checkUser: function (felhasznalonev, jelszo, callback) {
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
});
connection.query('SELECT COUNT(*) AS szamlalo FROM felhasznalok WHERE fhsz_email="' +
felhasznalonev + '" AND fhsz_jelszo= "' + md5(jelszo) +'";', function (error, results, fields) {
console.log(results[0].szamlalo);
callback(results[0].szamlalo);
});
connection.close();
}
编辑1: 我更新了我的代码,但仍然收到错误。
router.post('/', function(req,res,next) {
var l_status = 0;
console.log('l_status: '+l_status);
if (!req.body.email && !req.body.password) {
console.log('valami nincs kitoltve');
} else {
l_status = tools.checkUser(req.body.email, req.body.password, function(callback) {
return callback;
});
if(l_status == 1)
res.render('index');
else
res.render('login');
}
});
错误:
GET /bejelentkezes 304 1776.456 ms - -
GET /css/signin.css 304 5.524 ms - -
GET /favicon.ico 200 7.638 ms - 32038
l_status: 0
POST /bejelentkezes 500 728.901 ms - 2259
GET /css/404.css 304 13.212 ms - -
1 (this is checkuser output)
编辑2 - 更多信息
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');
var session = require('express-session');
var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var notfound = require('./routes/error');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set('trust proxy', 10);
app.use(session({
secret: 'E355A874F571C',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
// Authentication and Authorization Middleware
var auth = function(req, res, next) {
if (req.session && req.session.user === "denes" && req.session.admin)
return next();
else
return res.sendStatus(401);
};
// 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')));
app.use('/', index);
app.use('/rogzit', index);
app.use('/bejelentkezes', login);
app.use('/404', notfound);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
/*var err = new Error('Not Found');
err.status = 404;
next(err); */
req.redirect('/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;
的package.json:
{
"name": "e_wallett",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.17.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"morgan": "~1.8.1",
"pug": "~2.0.0-beta11",
"serve-favicon": "~2.4.2"
}
}
Adatbazis_v2.JS:
var mysql = require('mysql');
var md5 = require('md5');
var connection = mysql.createConnection({
host : 'localhost',
user : 'usr',
password : 'pw',
database : 'dbname'
});
module.exports = {
checkUser: function (felhasznalonev, jelszo, callback) {
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
});
connection.query('SELECT COUNT(*) AS szamlalo FROM felhasznalok WHERE fhsz_email="' +
felhasznalonev + '" AND fhsz_jelszo= "' + md5(jelszo) +'";', function (error, results, fields) {
console.log(results[0].szamlalo);
callback(results[0].szamlalo);
});
connection.close();
},
getTranzakciok: function () {
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
});
connection.query('SELECT * FROM tranzakciok";', function (error, results, fields) {
console.log(results[0]);
});
}
}
pug的第412行index.js:
// support callback API
if ('function' == typeof options) { //412th LINE
fn = options, options = undefined;
}
if (typeof fn === 'function') {
var res;
try {
res = exports.renderFile(path, options);
} catch (ex) {
return fn(ex);
}
return fn(null, res);
}
options = options || {};
options.filename = path;
return handleTemplateCache(options)(options);
};
我为我的英语错误道歉并感谢你的帮助。
答案 0 :(得分:0)
试试这段代码:
router.post('/', function(req,res,next) {
var l_status = 0;
console.log('l_status: '+l_status);
if (!req.body.email && !req.body.password) {
console.log('valami nincs kitoltve');
} else {
tools.checkUser(req.body.email, req.body.password, function(result) {
if(result== 1)
return res.render('index');
else
return res.render('login');
});
}
});
checkUser: function (felhasznalonev, jelszo, callback) {
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
connection.query('SELECT COUNT(*) AS szamlalo FROM felhasznalok WHERE fhsz_email="' + felhasznalonev + '" AND fhsz_jelszo= "' + md5(jelszo) +'";', function (error, results, fields) {
console.log(results[0].szamlalo);
return callback(results[0].szamlalo);
});
connection.close();
});
}
您只能在回调中使用传递给回调函数的结果。
我建议您在开始使用nodejs进行编码之前阅读一些关于异步编程的文档/教程。您可以从this网站开始。
编辑:checkUser函数的新代码
<强> G3ph4z 强>
谢谢你,我会读的。我尝试了你的代码,我收到了这个错误:GET /bejelentkezes 304 1733.043 ms - -
GET /css/signin.css 304 3.785 ms - -
l_status: 0
POST /bejelentkezes 500 714.691 ms - 2259
GET /css/404.css 304 3.594 ms - -
1
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:170:12)
at done (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:967:10)
at Object.exports.renderFile (C:\Users\denes\Documents\e_wallet\node_modules\pug\lib\index.js:412:12)
at View.exports.__express [as engine] (C:\Users\denes\Documents\e_wallet\node_modules\pug\lib\index.js:455:11)
at View.render (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\view.js:128:8)
at tryRender (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\denes\Documents\e_wallet\node_modules\express\lib\response.js:971:7)