Simple version of e-commerce project learning--The third step: database creation, registration function implementation and user password encryption

Source: Internet
Author: User

First, Introduction

After getting the data, in the home directory to create a DB directory to hold data, in the database to build a table to hold data, and then the page to get data to the database for query, you need to connect to the database table, and will be in the page to retrieve the data as query criteria to query the table data, which involves the MVC architecture, Now that you have view--v, controller--c, and then create the MODAL--M directory to store operations on the database

1. Let MONGO run in the project:

Mongod--dbpath./db     //If the current directory is./

2, in the modal file operation data:

Installation: Mongoose

CNPM Install Mongoose--save

To create a database:

varMongoose = require (' Mongoose '));//Createconnecting to a databaseMongoose.connect (' mongodb://localhost:27017/shopping ');//Get linksvarConnection =mongoose.connection;//failed to connect to databaseConnection.on (' Error ',function(err) {if(Err) {Console.log (' Connection failed ', Err)}})//Connection SuccessfulConnection.on (' Open ',function(err,data) {if(Err) {Console.log (' MongoDB connection error ', err); }Else{Console.log (' MongoDB connection success ');//Successful Connection Output   }})//Export DatabaseModule.exports = Mongoose;

Once you have the database, create a table for the Administrator admin

Create a table

varMongoose = require (' Mongoose '));//Create a data skeletonvarAdminschema =NewMongoose. Schema ({username:string, password:string, type:number})//Create model Usermodel = = "User table"varAdminmodel = Mongoose.model (' admin ', Adminschema);/** * Perform registered user data write * @type {{login}} * @params object username password*/functionReg (params) {adminmodel.create (params, (err,data)={      if(Err) {}Else{console.log (data); }})}module.exports={reg};

After it is created, because it is to store the administrator's data, it is called during the registration operation to save the user's data to the data table.

varMongoose = require ('./connect.js '));//Create a data skeletonvarAdminschema =NewMongoose. Schema ({username:string, password:string, type:number})//Create model Adminmodel = = "Admin TablevarAdminmodel = Mongoose.model (' admin ', Adminschema);/** * Perform registered user data write * @type {{login}} * @params object username password*/functionReg (params) {adminmodel.create (params, (err,data)=>{//after having the data to be submitted, it is called to add the Reg method that registers the user information.       if(Err) {Console.log (' Err '); }Else{console.log (data); }})};module.exports={reg};

Then restart the console, register red run on over!!!

Simple version of e-commerce project learning--The third step: database creation, registration function implementation and user password encryption

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.