Node.js Notes (vii) Read MONGODB data and display __JS

Source: Internet
Author: User
Tags mongodb

Although previously contacted the content of MySQL, but do the website, feel NoSQL database or go around.
Many books have mentioned the use of Node.js and MongoDB, here to record the learning process.

Basic commands for MongoDB:

Use dbname  new or using an existing database
show DBS display all the database show
collections display all collection (table)
Db.collection.find () Print data in a table

These commands are used in the MONGO console and cannot be used directly in Node.js.

We want to take out the list of articles stored in the database to display on the page, without the template engine, I used the Ejs and Jade two engines, used to compare
On the management of the database, you can directly manage or use the middleware provided by Monk, I will post it separately.

* * *app.js does not use monk * * */var express = require (' Express ');
var path = require (' path ');
var jade = require (' Jade ');
var fs = require (' FS ');
var MongoDB = require ("MongoDB");

var app = Express ();
View engine Setup App.set ("Views", Path.join (__dirname, ' views '));

App.set (' View engine ', ' Jade '); Not use monk var server = new MongoDB.  
Server (' localhost ', 27017,{auto_reconnect:true}); var db = new MongoDB.
Db ("MySite", Server,{safe:false});  
        Open the database connection Db.open (function (err,db) {if (err) {Console.log (err);  
    return false;
else Console.log (' connect! ');
}); Connect to collection var link = function (db) {return function (req,res) {db.collection (' usercollection ', {safe:true}, Fu Nction (Err, collection) {/*collection method is used to connect existing tables, {safe:true} option, when collection does not exist the error CreateCollection method is used to create a new table, {Safe:t
     Rue} option, when collection exists, error * * (ERR) {Console.log (err);
         } else{Console.log (' Get collection! '); Collection.fIND (). ToArray (function (err,docs) {console.log (' find ');
         Res.render (' UserList ', {userlist:docs});
     });
    }
    });
} app.get ('/', link (db)); /* About the location of the routing link method, the appropriate approach is to put in the Routes/index file, but the method of the dependencies are too many, so it is still stuck in a file * * * var list = function () {console.log (' hah ');} var
Port = 8080;
App.listen (port);

 Console.log (' Listening on port ' + port);
/* Use monk*/
var express = require (' Express ');
var path = require (' path ');

var jade = require (' Jade ');
var fs = require (' FS ');
var MongoDB = require ("MongoDB");
var monk = require (' monk ');
var db = Monk (' Localhost:27017/mysite ');
var app = Express ();

View engine Setup
app.set ("views", Path.join (__dirname, ' views '));
App.set (' View engine ', ' Jade ');

var userlist = function (db) {return
    function (req, res) {
        var collection = Db.get (' usercollection ');
        Collection.find ({},{},function (e,docs) {
            res.render (' userlist ', {'
                userlist ': Docs
            });//using page engine rendering
        });
    };
};

App.get ('/', userlist (db));

var list = function () {console.log (' hah ');}
var port = 8080;
App.listen (port);
Console.log (' Listening on port ' + port);

You can see that using monk eliminates the open operation and the code length is shortened.

Jade and Ejs
Need to display the list data on the page, so the page engine needs rendering
Here's the code for EJS and Jade:

/*jade 
to indent control Strictly, a little attention on error/
extends layout block
content
    H1.
        User List
    ul each
        user, I in userlist
            Li
                A (href= "Mailto:#{user.email}") = User.username
/*ejs
This template is on the Internet to find, has not tried/
*

————————————————————————
About the classification of Nodejs articles, think about it can not be regarded as a front-end, also not a traditional back-end.
But the advent of Nodejs is really good news for the front-end engineers who are really familiar with JavaScript. So classify it as the front end, although there are some irregularities, but also do.

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.