Want to write a blog for a long time, because has been suffering from lazy cancer, so has been dragged to the present. Markdown's grammar is also just learning, test the effect
All right, no, it's just dry.
------------------------------------------------------------------------------------------------
1. Preparatory work
Installation of Nodejs, Express and MongoDB (the latest version is installed here)
# # Install: # # #
Nodejs installation is nothing to say. However, it may be problematic for students who have successfully used the original version after the installation is complete.
The original version is: $ node-v The new version is not supported in $, directly on the command line: node-v
After installing the latest version of Express, you need to install: Express-generator//express Command Tool, many beginners will encounter this problem (of course I am)
The MongoDB installation will be explained below.
Create a framework for a project with Express.
This I created a website named Chihou, with the template engine is Ejs.
>express-e Chihuo
-E represents the Ejs template engine and does not write the-e default create Jade template engine
Then in the input:
CD Chihuo//Enter into the project directory you created
NPM Install//Read the Package.json file in the root directory and then install the package that the project depends on
Then through the CMD command line CD to the Chihuo directory
>node app
This will start the project.
Tip 1: If you cannot start the project: Look at the App.js file without a listening port. If not,
In module.exports = app; Statement before adding App.listen (3000);
Tip 2:ctrl + C to terminate the run
Then we can http://127.0.0.1:3000/or http://localhost:3000 in the browser's address bar.
This is your first express-created node app.
Here you have completed an important step in the project.
After that, we start with the catalogue.
Packages that are dependent on the--node_modules project
--public Directory of public Resources
--routes A scientific route with some routing files in it.
--views is the page file.
The entry file for the--app.js project. Of course you can change it to another name.
2. The basic results are now available, then the database is created.The installation of MongoDB
Only the installation of window is described here
Download the zip on the official website, this I installed in the MongoDB directory of D Drive
1. Create the MongoDB folder in the D drive and then unzip the downloaded package and copy the Bin folder to the created MongoDB folder.
2. Then create a data folder in the MongoDB folder, and then create the DB folder in the Data folder
3. Open cmd command line
>d:
>CD Mongodb\bin
>mongod-dbpath D:\mongodb\data\db
4. On the open a CMD command line:
>d:
>CD Mongodb\bin
>mongo
5. It's ready to use.
And then we're going to design our database.
In the MongoDB database that you just opened, enter:
>use Chihuo \ \ Create a database called Chihuo
>db.createcollection ("users") \ \ Create a collection
>db.users.insert ({"Name": "admin", "Password": "111"}) \ \ Adds a document to the Users collection.
>db.users.find () \ \ Query the document you added
And then you connect to the database that you just created in your project.
Create a folder under the project root directory database, and then create a
Db.js
var mongoose = require (' Mongoose '); var db = Mongoose.connect (' Mongodb://localhost/chihuo ');//; connection database var Schema = Mongoose. Schema; Create model var Userschema = new Schema ({5 name:string,6 Password:string7}); A new model has been defined, but this pattern has not been associated with the Users collection Exports.user = Db.model (' users ', Userschema); Associating with the Users collection
3. Then create the view file in the Views folder
The project view file we created above with Express is the Ejs suffix, and we are generally accustomed to using HTML suffix names.
So how do we get him to identify the HTML view file?
Found in the App.js file
>app.set (' View engine ', ' Ejs ');
Replace it with:
>app.set (' View engine ', ' html ');
Then use the App.engine () method to register the suffix name of the template engine. Code:
>app.engine ('. html ', require (' Ejs '). __express);//The underscore inside is two
Then we create a login.html (Landing page), index.html (original, change the suffix name on the line), Ucenter (after landing the page);
index.html--<! Doctype html>
login.html--<! Doctype html>
ucenter.html--<! DOCTYPE html>
4. Finally, the routing control, in the routes file of the Index.js var express = require (' Express '); var router = express. Router (); var user = require ('.. /database/db ') .user; /* get home page. */ Router.get ('/', function (req, res) { res.render (' Index ', { title: ' index ' }; }); /*login */ router.get ('/login ', function (req, res) { Res.render (' Login ', { title: ' login ' }); }); /* Ucenter*/ router.post ('/ucenter ', function (req, res) { var query = {name: req.body.name, password: req.body.password}; ( function () { user.count (query, function (err, doc) { if (doc == 1) { console.log (query.name + ": Landing success " + new date ()); res.render (' Ucenter ', { user:doc }); }else{ Console.log (query.name + ": Landing failed " &NBSP;+&NBSP;NEW&N Bsp;date ()); res.redirect ('/'); } }); &nbs P }) (query); });31 32 module.exports = router;
At present, so much to explain, after the blog I will explain the latest version of the session problem and the front page application to bootstrap, etc., constantly improve the project.
GitHub Address: Https://github.com/songtanjichuan/chihuo_0.0.1.git
Mongoose Reference Address: HTTP://WWW.HUBWIZ.COM/COURSE/543B2E7788DBA02718B5A4BD
If you feel good, please recommend. If reproduced, please add the original link.
node. js + MongoDB