简体   繁体   English

Node.js,MongoDB登录表单

[英]Node.js, MongoDB Login form

I have a node.js application with a connection to a remote mongoDB server. 我有一个与远程mongoDB服务器连接的node.js应用程序。 The database contains some custom codes that have been pre-created and distributed to certain users. 该数据库包含一些预先创建的自定义代码,并已分发给某些用户。 The idea is that only those who enter one of such codes into a form on the index page can be allowed to view the rest of the site. 这个想法是,只有那些在索引页面上的表单中输入其中一种代码的人才能被允许查看网站的其余部分。 That is, I want to cross-reference the code entered with the master list stored in my database. 也就是说,我想交叉引用与数据库中存储的主列表一起输入的代码。

This is an example of what I'm trying to do(note this is in routes/index.js): 这是我要执行的操作的一个示例(请注意,这在routes / index.js中):

collection.findOne({ "Code": "fooCode" }, function(err, doc){
   if you cannot find fooCode
       show an error message, clear the input area and tell the user to re-enter a correct code

   if you find fooCode
       redirect to a new page;
});

The above code is within a router.post('/', function(req, res){...}) function. 上面的代码在router.post('/',function(req,res){...})函数中。

My question is, how do I clear the text input area(without refreshing the page) and ask the user to re-enter a new code when a wrong code is entered? 我的问题是,当输入错误代码时,如何清除文本输入区域(不刷新页面)并要求用户重新输入新代码?

Secondly how do I redirect the user to a new page on valid entry of a code? 其次,如何在有效输入代码的情况下将用户重定向到新页面? Thanks. 谢谢。

  1. For that kind of behavior you would need to submit the form via XHR, parse the (JSON) response, and then clear the form on error and display error information. 对于这种行为,您将需要通过XHR提交表单,解析(JSON)响应,然后清除错误表单并显示错误信息。 On the server side you can simply do something like res.json({ status: 'success' }); 在服务器端,您可以简单地执行诸如res.json({ status: 'success' }); or res.json({ status: 'error', error: 'Bad token' }); res.json({ status: 'error', error: 'Bad token' });

  2. On success, you could do the redirect in the browser via window.location.replace("http://example.org/foo"); 成功后,您可以通过window.location.replace("http://example.org/foo");在浏览器中进行重定向window.location.replace("http://example.org/foo"); or if you want the current page in the session history (eg reachable with the browser's back button) you can use window.location.href = "http://example.org/foo" . 或者如果您想在会话历史记录中使用当前页面(例如,使用浏览器的后退按钮可以访问),则可以使用window.location.href = "http://example.org/foo"

  1. To clear the input: I would handle this on the front-end, so that everytime the button is clicked (to send the form) it clears the input with: 清除输入:我将在前端进行处理,以便每次单击按钮(发送表单)时,都会使用以下命令清除输入:

    <input id="userInput" type="text"> <button id="submitBtn">Send</button>

Then in your script: 然后在您的脚本中:

// Cache DOM
const myInput = document.getElementById('userInput');
const myBtn = document.getElementById('submitBtn');

// Event Bind
myBtn.on('click', clearInput);

// Functions
function clearInput() {
    myInput.value = '';    
}

2. To redirect the user to a new page 2.将用户重定向到新页面

res.redirect('/<path>');

You can do the following in your routes to redirect user to the home route. 您可以在路由中执行以下操作,将用户重定向到本地路由。

collection.findOne({ "Code": "fooCode" }, function(err, doc){ 

    if err throw err 

    else {    
        res.redirect('/');
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM