웹 프로그래밍/SQL

redirect 하려면 res.end()하기 전에 302 location 고치기

mcdn 2021. 9. 24. 17:32
반응형

https://stackoverflow.com/questions/34101219/redirect-after-res-end-in-nodejs

 

Redirect after res.end() in nodejs

i am using jsonwebtoken to authenticate user. my plan is to save the token at header and then get the header value before any request made . yes i did save the value in header at login successfull...

stackoverflow.com

https://stackoverflow.com/questions/27366107/second-select-query-if-first-select-returns-0-rows

 

Second SELECT query if first SELECT returns 0 rows

I am trying to speed up a PHP script and I am currently pushing some PHP logic in the Mysql domain of the thing. Is there a way to make a different select query if the first Select returns no rows,...

stackoverflow.com

exports.user = function (req, res, queryData) {
	db.query(`SELECT * FROM users`, function(err, users) {
        if (err) {  throw err; }
        db.query(`SELECT * FROM users WHERE id=?`, [queryData.id], function (err2, user) {
          if (err2) {throw err2; }
		  if (user.length <= 0) {
			  res.writeHead(302, {Location: `/`});
			  res.end();
		  } else {
			console.log(queryData.id);
			console.log(user);
			var title = user[0].name;
			var description = user[0].email;
			var list = template.list(users);
			var html = template.HTML(title, list,
				`<h2>${title}</h2>${description}`,
					` <a href="/create">create</a>
						<a href="/update?id=${queryData.id}">update</a>
						<form action="delete_process" method="post">
						<input type="hidden" name="id" value="${queryData.id}">
						<input type="submit" value="delete">
						</form>`
			);
			res.writeHead(200);
			res.end(html);
		  }
        });
      });
}

 

userid가 없는 경우 그냥 오류만 났는데 

length를 체크해서 해결해다 

 

 

 

 

반응형