I'm trying to implement the JSON TOKEN in node.js but I have this error:
[ERR_HTTP_HEADERS_SENT]: Can not set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js: 469: 11)
This is the code I have:
router.post('/api/v1/Login', jsonParser, function(req, res){
if(!req.body)
return res.sendStatus(400)
console.log(req.body);
var UsuarioReg = req.body.user;
var ContraReg = req.body.pass;
console.log("User: "+UsuarioReg + " Contraseña: "+ContraReg)
var request = new sql.Request();
try{
request.query("SELECT * FROM dbo.[Client] WHERE username = '"+UsuarioReg+"'AND pass = '"+ContraReg+"'", function (err, recordset) {
if(err){
console.log(err);
}else{
if(recordset.rowsAffected > 0){
res.write(JSON.stringify("Usuario identificado correctamente"))
console.log("Usuario identificado correctamente")
const user = {id: 3};
const token = jwt.sign({ user },'my_secret_key');
res.json({
token:token
});
}
}
})
}catch(err){
res.send(JSON.stringify("Error while querying database :- "+err))
console.log("Error while querying database :- "+err)
}
});
I do not understand why this error is due. They help me? Thanks !!!