This is a code that works by checking if the inputs someone registered in login page exists in the database or not. It works and all. However, when I wanted to set a cookie(I just wanted to set it randomly so I see if it works) so that I can save its state as online, an error has happened
Cannot set Headers after they are sent
I understand from this that is because I did res.render
and res.cookies
at the same request or something like that, however, I removed res.render
and kept res.cookies
, the error is gone but even then it didn't register any cookies(No cookies at network panel of dev tools nor by writing document.cookie
at console)
I also tried to move that code to the request get when rendering the page but it still doesn't work.
This is the server code:
// Importing modules/packages
let connection = require("./Connection.js");
let bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
let express = require("express");
let app = express();
app.use(cookieParser());
let urlencodedParser = bodyParser.urlencoded({ extended: false });
// Server and routing
app.listen(3000, function() {
console.log("The server at port 3000 is open!");
});
app.set("view engine", "ejs");
app.get("/Signup", (req, res) => {
res.render("Register");
});
app.get("/", function(req, res) {
res.redirect("Login");
});
app.get("/Login", function(req, res) {
res.render("Login");
});
// Database's Connection
connection.connect(err => {
if (err) {
throw "ERROR! " + err;
}
console.log("Has connected to MySQL Database!");
});
// Requesting and Receiving Data from body parser
app.post("/Signup", urlencodedParser, function(req, res) {
console.log(req.body);
res.render("Register");
connection.query(
`insert into users (Username, User_Password, Email_Address) values ("${
req.body.Username
}", ${req.body.Password}, "${req.body.Email}")`,
function(err) {
if (err) throw "An error has happened while excuting the query " + err;
console.log("Query got excuted successfully!");
}
);
});
app.post("/Login", urlencodedParser, (req, res) => {
connection.query(
`SELECT Username FROM users WHERE User_Password = ${
req.body.Password
} AND Username = "${req.body.Username}"`,
function(err, results) {
if (results.length === 0) {
console.log(
"Sorry, this username/password doesn't exist in our database."
);
} else {
console.log("Logined successfully!");
res.cookie("Friend", "Jack");
}
}
);
res.render("Login");
});
I also tried to read the cookies by using req.cookie("Friend")
but it didn't work. I don't know what's the problem.
How I can solve this please?