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