admin管理员组文章数量:1392007
I'm doing a basic login from my node.js with mysql.
In my code example, if the session is correct send me to an html page called "home", but for this case, I need that once you make the session, my Javascript client sends an alert that says you have logged in or if its user is incorrect. And if it is correct, save the word "correct" in a variable that I can later use in my javascript file
How could I send that execution, so that my javascript file sends an alert?
app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?',
[username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.redirect('/home'); //Here I want to change the instruction so that my javascript file sends an alert and create a varible that I can use in my file Javascript
} else {
console.log('Incorrect Username and/or Password!');
}
response.end();
});
});
Thanks
I'm doing a basic login from my node.js with mysql.
In my code example, if the session is correct send me to an html page called "home", but for this case, I need that once you make the session, my Javascript client sends an alert that says you have logged in or if its user is incorrect. And if it is correct, save the word "correct" in a variable that I can later use in my javascript file
How could I send that execution, so that my javascript file sends an alert?
app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?',
[username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.redirect('/home'); //Here I want to change the instruction so that my javascript file sends an alert and create a varible that I can use in my file Javascript
} else {
console.log('Incorrect Username and/or Password!');
}
response.end();
});
});
Thanks
Share Improve this question asked Dec 26, 2018 at 17:28 Isaac AlejandroIsaac Alejandro 2471 gold badge4 silver badges8 bronze badges 8- are you using a template engine ? – Chiller Commented Dec 26, 2018 at 17:36
- @Chiller no, how can I use that? – Isaac Alejandro Commented Dec 26, 2018 at 17:37
-
response.json({message: "your message goes here", status: 200 //4XX for any error})
then in javascript (front-end) you will get inthen
orcatch
based on your response. – Shailesh Rathod Commented Dec 26, 2018 at 17:42 - @ShaileshRathod, Do you have any example? – Isaac Alejandro Commented Dec 26, 2018 at 21:31
- @IsaacAlejandro How do you manage your front-end, do you use any framework, or just send static html files by NodeJS? – Slawomir Wozniak Commented Dec 26, 2018 at 21:47
3 Answers
Reset to default 3Here's a dirty trick you can use,but it will re-render the ejs view:
On server end(using express):
app.get('/route', function (req, res) {
res.render('view', { errormessage: 'your message' });
});
On client side(using ejs templates):
1.create an invisible div on your ejs template ,to access your ejs variable sent by server:
HTML:
<div id='status' class='invisible'><%= errormessage %></div>
CSS:
.invisible {
display: none;
visibility: hidden;
}
2.add this js script(jquery) at end of ejs to access this variable and create an alert:
<script>
if($('div#status').text()!='')
{
alert($('div#status').text());
}
</script>
At server side(Express middleware):
if(userNotExist) return res.status(401).end('Incorrect Username and/or Password!');
Handle at Client side.
Angular:-
$http().....
.error(function(data, status) {
console.error('Repos error', status, data);//"Repos error" 500 "User already exists."
});
jQuery:-
$.ajax({
type: "post",
url: url,
success: function (data, text) {
//success code here.
},
error: function (request, status, error) {
//error handling here.
}
});
There is no way to directly execute code on client side, by sending a directive from the server. What you want to do, is to send an information, and interpret it properly on the client.
For instance, you can use sessions
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
request.session.makeAlert = true;
response.redirect('/home');
}
In the /home route
app.get('/home', function(req, res){
const dataYouSend = {};
if (req.session.makeAlert) dataYouSend.makeAlert = true;
res.send(dataYouSend);
});
And finally in the JS code on /home route, use your data
fetch('adress/to/your/home')
.then(response => {
// It may not be precisely this, I don't know how you send data
if (response.makeAlert) alert("Alert!!!");
})
.catch(err => {
if(err) console.log(err);
});
本文标签: How can I send one alert from my NodeJS to my Javascript clientStack Overflow
版权声明:本文标题:How can I send one alert from my Node.JS to my Javascript client? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744745400a2622845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论