Use node. js + mongodb to set up a simple login registration function.

Source: Internet
Author: User

Use node. js + mongodb to set up a simple login registration function.

Preface

Recently, I suddenly became interested in databases and the backend, and began to learn for a long time. I think I am only a front-end and only use java scopep, so I started to look at nodejs, looking at it, I suddenly found that mongodb and nodejs are better equipped !, Then I started my mongodb journey. I won't talk much about it below. Let's take a look at the detailed introduction.

Mongodb Introduction

Simply put, mongo is a nosql database that does not use the SQL syntax. Of course, it is also similar. addition, deletion, modification, and query are similar, however, in terms of concept, mongo is quite different from mysql. For example, mongo does not have a table but a set. Because of the many conceptual things, I will attach a link to you here. You can take a look at biu! I am a link !;

Let's talk about some of the things I use in my demo.

Directly copy the dependent image of the package.


The role of each part is described as follows:

  • Express: This is a web application development framework of nodejs;
  • Body_parser: this should be known to anyone who writes nodejs. It is a middleware used to parse the content in the body of the client request. Because form submission is required, the data to be parsed must be obtained;
  • Mongoose: From this name, we can know that this dependency is related to mongodb. This dependency is encapsulated by nodejs to connect to and operate mongodb databases;
    I believe you will all know the rest of the articles. In fact, I believe that I will not describe all of them. Forgive me !!!

List structure list

Please refer to the image directly! I really don't want to generate a tree!


In fact, this is a very simple demo! We mainly try to use nodejs to render static front-end resources and nodejs routes, as well as its support for mongodb!
In user. js, a Schema of mongoose is determined, that is, the format of data written to the database.

var UserSchema = new Schema({ username : String, userpsw : String, logindate : Date})

In this way, a data model is built;

From the project directory, we can see that I only wrote three simple pages. In fact, the most important thing is a file, app. js;

App. js content

In this section, I mainly wrote about how to store incoming data to mongodb, how to determine whether the data has been registered, and whether the correct account and password have been entered;

Here I put a piece of registration check code. After successful registration, I will write user information to the database.

App. post ('/register', (req, res) => {console. log ('user in session') console. log (req. session); // first query whether the user console exists. log ("req. body "+ req. body); var UserName = req. body. username; var UserPsw = req. body. password; // verify var updatestr = {username: UserName}; res. setHeader ('content-type', 'application/json; charset = UTF-8 ') console. log (updatestr); userSchema. find (updatestr, function (err, obj) {if (err) {console. log ("Error:" + err);} else {if (obj. length = 0) {insert (UserName, UserPsw); res. send ({status: 'success', message: 'true'})} else {res. send ({status: 'success', message: 'false '})}}})});

In this way, the user's incoming information can be stored in the database. Similarly, the user logs on to the database to check whether the data has been transmitted, then compare the usernames and passwords one by one. If they match, the logon is successful;

Front-end

Forgive me for being really limited in my ability to express myself. You may not be able to understand it at the backend. Please refer to my source code. Next, let's take a look at some of my front-end operations;
The front-end is mainly on the login page. I made a cookie processing. When the user clicks to save the account, I will store the user information in the cookie. Next time I log on, I don't have to bother entering the user name and password;

      if($('#mycheck').prop('checked')==true){      setCookie('username',username,7);     }

In fact, when the login verification is successful and the user checks the checkbox, the cookie is saved; the next login will directly resolve the cookie value, and then put it into the value of the input box of the user name!

Due to my limited expression ability, the article may not be so detailed, you can look at the source code if you do not understand

Github Project address

Local download

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.