Skip to content

Instantly share code, notes, and snippets.

@aktasfatih
Last active July 10, 2019 22:02
Show Gist options
  • Save aktasfatih/f067fdad0b2a29537dbb703d7a675412 to your computer and use it in GitHub Desktop.
Save aktasfatih/f067fdad0b2a29537dbb703d7a675412 to your computer and use it in GitHub Desktop.
Route example
router.post('/userInfo', function(req, res){ // Route for handling the request
jwt.verify(req.body.token, JWT_SECRET, (err, decoded) => { // Checking if the JWT token of the user is valid
if (err){ // Handling error
res.json({
"info" : "Error0"
});
}else{
if(typeof decoded.username === "undefined"){ // Checking if username is defined in the request
res.json({ // Handling the error
"info" : "Error1"
});
}else{
var query = "SELECT u.NAME AS name, u.last_name, u.about, s.NAME AS school, u.profile_pic, u.goldMedals, u.silverMedals, u.bronzeMedals FROM Users u\
LEFT JOIN Schools s ON u.School = s.school_id WHERE u.username = ?"; // MYSQL query
connection.query(query,[decoded.username], function(error, rows, fields){ // Querying
if(error){ // Handling any error occured during querying
console.log("There was an error: " + error.stack);
res.json({res:false});
return;
}
var info = rows[0]; // Returning the query.
res.json(info);
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment