Front-end processing server that is very painful, beginner node, although feeling unusually strong, but it is still a bit difficult to learn, node is a tool, it is not omnipotent, build a system or need some other tools, for me this is not how to contact the front end of the server, the challenge is some. Yesterday, refer to some information to try to build a simple landing system with NODE+EXPRESS+MONGODB, in this note.
Express is a flexible NODEJS Web application framework that provides a range of powerful features to help you create a variety of Web applications.
MongoDB is a database.
1, installation Express,express installation is relatively simple, directly with NPM install-g Express-generator, must use this, if using NPM Install-g Express will appear express not found problem.
2, the installation of MongoDB, under the official website under the program, and then step by step installation, I was in the D disk new a MongoDB folder, installed in the folder.
3. Copy the files below the bin in the installation file directory to the MongoDB root directory.
4. Start the Mongodb,cmd command as follows:
If you see the following content, congratulations on the success of the launch. It is better to build a cmd file each time you start running the file.
Mongodb.cmd file:
: : Navigate to D drive D::: Switch to MONGODB database directory CD MongoDB:: Delete database lock record file if --dbpath "D:\Mongodb\data"
5. Run MONGO in the MongoDB directory, link to test by default
Here the entire environment is configured and started.
See the specific node below.
Running Express login-e in a directory after you install Express will automatically create a project, and NPM install will download the dependent package.
1. Create a new models folder and create a new user.js under this folder:
var mongoose = require ("Mongoose"); // The top Conferencing User component var Schema = Mongoose. Schema; // Create model varnew Schema ({ userid:string, // a new model has been defined, but this pattern has not been associated with the Users collection // associating with the Users collection
2, under the views built Index.ejs, Errors.ejs, Login.ejs, Logout.ejs, Homepage.ejs. (index is self-brought, not built)
Index.ejs:
<! DOCTYPE html>
Login.ejs:
<! DOCTYPE html>
Loginout.ejs:
<! DOCTYPE html> setTimeout (function() { ="/"; ); </script> </body>
Homepage.ejs:
<! DOCTYPE html>
3. Configure the route at Routes Index.js:
varExpress = require (' Express ');varRouter =Express. Router ();varMongoose = require (' Mongoose '));varuser = require ('.. /models/user '). User;mongoose.connect (' Mongodb://localhost/admin '); /*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 ' });}); /*Logout*/Router.get ('/logout ',function(req, res) {Res.render (' Logout ', {title: ' Logout ' });}); /*Hompage*/Router.post ('/homepage ',function(req, res) {varQuery_doc ={userid:req.body.userid, password:req.body.password}; (function() {User.count (Query_doc,function(err, doc) {if(doc = = 1) {Console.log (Query_doc.userid+ ": Login success in" +NewDate ()); Res.render (' homepage ', {title: ' homepage ' }); }Else{console.log (Query_doc.userid+ ": Login failed in" +NewDate ()); Res.redirect (‘/‘); } }); }) (Query_doc);}); Module.exports= Router;
Done.
Make sure that you start MongoDB and run NPM start under the login project. Then you can see the interface in the browser input https://localhost:3000:
Missing a bit, you need to create a username and password in MongoDB, or you can't log in. The steps are as follows:
From scratch, use Nodejs+express+mongodb to build a simple landing system