This Demo is based on the project of the cainiao tutorial, please go to the official website of the cainiao tutorial to view the relevant nodeJS tutorial, perform the actual operation according to the tutorial, and then implement the login and registration functions by yourself. This Demo is for reference only, and does not comply with the relevant front-end specifications.

The technology stack used

node+express+mongodb

Project directory structure

  • Node_modules: directory of a third-party module
  • Public: public file directory (JS, CSS, image)
  • Login. HTML: indicates the login page
  • Register. HTML: registration page
  • Main. HTML: main page
  • Db.js: database related encapsulation (database add, query)
  • Login. js: interface startup file (login, registration interface)

Login scenarios

(1) The user name cannot be empty. (2) The password cannot be empty; (3) If the user name and password are not empty, query the database to check whether the user exists. If yes, check whether the user name and password are the same and the login is successful. Inconsistent return user name or password error; If no, the system returns that the user does not exist.

Registration scene

(1) The user name cannot be empty. (2) The password cannot be empty; (3) If the user name and password are not empty, query the database to check whether the user exists. If yes, the system displays that the user already exists and can log in directly. If not, register the user and insert it into the database.

Partial operation demonstration

The login

The home page

registered

Inserting a database

Project initialization

1. Create a directory

Create a directory, for example, node-login

2. Install Express

Install Express and save it to the dependency list: Open CMD command line, navigate to the project directory, use CNPM install Express –save command line as shown below:

The above commands will install the Express framework in the node_modules directory of the current directory, where the Express directory will be created automatically. There are several important modules that need to be installed with the Express framework:

  • Body-parser-node. js middleware for JSON, Raw, Text and URL-encoded data.
  • Cookie-parser – This is a tool that parses cookies. Req. Cookies can be used to retrieve cookies that have been passed and turn them into objects.
  • Multer-node. js middleware for processing form data encType =”multipart/form-data” (setting the MIME encoding of the form).

The following command is used:

cnpm install body-parser --save
cnpm install cookie-parser --save
cnpm install multer --save
Copy the code

The dependent modules are shown as follows:

3. Install MongoDB

Run the CNPM install mongodb command

4. Create a folder public

Create a new public folder to store public file directories (js, CSS, image)

Github address:node-loginWelcome start.