This is a creation in Article, where the information may have evolved or changed.
A brief introduction to these three things under Node+express+mongodb.
Node: Is running on the server side of the program language, the surface of the past is the same thing as JavaScript, but, really is the server language, the individual feel at a certain level than C flexible, Java does not mention. Anyway, you just have to think that node can do a lot of things, and it's definitely not just web development.
Express: This is a frame of node, node has a lot of open source framework, Express is a great God developed (this God has moved to the development of the Go language). Express can make it easier for you to operate node (because native node is cumbersome to write, and because node is event-driven , there are many asynchronous callbacks, and it's a lot more than just looking dizzy ...). )
MongoDB: This is a non-relational database (NoSQL), too deep things I do not know, anyway, this goods also have a very strong place, the disadvantage is not suitable for high data consistency requirements such as financial development. But the advantages are fast .
Summary: This means that node and MongoDB combine to be particularly suitable for a scenario- fast, high -throughput situations.
Let's start with the preparatory work: (Take the windows8.1 system environment as an example).
1.node: First download installation Nodejs, download the address http://www.nodejs.org/, installed after checking whether the system environment variable is automatically configured path, if not, please configure node's installation address to the path. Then in the CMD to enter node, if you can, then this step is OK, very simple.
2.express: This installation is divided into two kinds, one is the global installation, one is the local installation. The network said iffy iffy, but in practice you will find different system environment problems, such as win8.1 Chinese user name situation, you have to change NPM global path (NPM is the Node module package management program, when you installed node in your own NPM), Operation:
prefix = Custom Module directory
Cache = Custom caching Directory
Then install Express (new version 4. x above to install express-generator)
NPM I-g Express (old version npm i-g express+ version number)
NPM i-g Express-generator
(If you want to install it locally in the same directory as NPM, first CD to the Node_modules directory, then use the npm I express-generator command, and then configure the./bin in the Node_modules directory to the environment variable path)
Test:
Express-v success will show the version number
3.MONGODB: This database installation is very simple, download address http://www.mongodb.org/. Install it, right in the CMD CD to the bin directory under the MongoDB installation directory, and then tap the command:
Mongod--dbpath= "MongoDB installation directory \data"--logpath= "MongoDB installation directory \log\log.txt"--install--servicename MongoDB-- Servicedisplayname MongoDB
When you're done, you'll find that there's a MongoDB service in your computer's service, yes, that's it, and then you can run the service.
the point: building a simple Node+express+mongodb project
In the CMD console, first CD to a directory, remember this your workspace, then use Express to create an app project
Express HELLO-WORLD-E(-e means support for the Ejs template engine, which is Jaden by default.) What's the template engine, like JSP ... It's too deep for me to understand. I am more good at HTML native things, like this template engine I also used for the first time, but also quite convenient oh, but in my opinion, no use, I do not need, but may you need ... )
Then we download the dependency package again
NPM i(This will automatically install the dependent modules that the project needs into the project's modules)
We CD to the Hello-world directory, is with the command
NPM StartStart the project (also can be node/bin/www, the old version directly node App.js, because it depends on the Package.json in the boot configuration)
We can http://127.0.0.1:3000/in the browser address bar This is your first express created node app.
Isn't it very ha-pi ...
We study the next express Create project
The main contents of the project you need to know are: routes and views, you'd better create a new directory in the project called models (after the function)
The routes index.js configuration is the path mapping relationship for Get and post requests, which is very simple.
Views in the Index.ejs is equivalent to an HTML file, which is some HTML tags and <%%> tags, feel and jsp almost oh.
Looks good, the standard MVC framework (models model, views inside display, routes inside put control)
We've built the app prototype, and we're designing the database
CMD command line:
MONGOEnter the database
Use Hello-worldCreate a Project Database
db.adduser ("Shuaige", "123456")To this database to create a name of the handsome guy's account, password 123456 (but I think maybe I understand that is not in place, you can not do this operation)
Then we create collection for the Hello-world database (collection is equivalent to the table in Oracle and MySQL)
db.createcollection ("Users")Create a collection, which is the table
Db.users.insert ({userid: "admin", Password: "123456"})Add a document to users, which is a record account admin, password 123456
OK, now check:
Db.users.find ()If you see the documentation you just added, OK
Good simple database collection and document setup, we'll go back to the node project created by Express, and we need:
Create a user.js under models as the Users collection for the Entity class mapping database
do a few pages under the views (can use EJS can also use HTML, I will use Ejs bar)
index.js configuration route under routes, that is, request mapping processing
1 Create a user.js under models as the Users collection for the Entity class mapping database
User.js
var mongoose = require ("Mongoose"); // Top Conferencing User component Var schema = mongoose. schema; // Create Model Var userschema = new schema ({ userid: string, password: string});// defines a new model, However, this pattern has not been associated with the Users collection Exports.user = mongoose.model (' users ', userschema); // Associated with the Users collection
2 under 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>
logout.ejs
<! doctype html>
Homepage.ejs
<! DOCTYPE html>
error.ejs Error page, I did not do, you are interested to try to play on their own.
3 Configure routing in Index.js under routes, that is, request mapping processing
Index.js
Var express = require (' Express '); var router = express. Router (); Var mongoose = require (' Mongoose '); Var user = require ('. /models/user '). User;mongoose.connect (' Mongodb://localhost/hello-world '); /* 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) { var query_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 " + new date ()); res.render (' homepage ', { title: ' homepage ' }); }else{ Console.log (query_doc.userid + ": login failed in " + new date ()) ; res.redirect ( '/'); } }); }) (Query_doc);}); module.exports = router;
OK, basically done, you can try.
Here's how to debug the server-side code:
We'd better use a kit called Node-inspector.
NPM i-g node-inspector//Installation Node-inspector
And then run it in CMD.
Node-inspector
Open a new CMD,CD to the project Hello-world directory
Node--debug./bin/www (or node--debug-brk./bin/www, the node program created by the previous version of Express, please use node--debug app.js)
Open http://127.0.0.1:8080/debug?port=5858 in the browser
Then new window opens Http://127.0.0.1:3000/
The browser can debug server-side code.
--------------------------------------------------------------------------------------------------------------- ----------------
Bo Master PostScript--
"This is a good note+mongo+express introductory article, and the version and command are not very accessible from the current version. ”
1. Express Template NPM (NPM i) dependencies need to execute commands in the same directory as the configuration (package.json) file
2. MONGO and express need to install dependencies Mongoose,Express(npm install Mongoose NPM install Express)
3. Start a project with NPM Strat When creating a new project , but the second time you execute the project, you need to start the project with the node./bin/www command (unexplained)
4. Bloggers do not use node-inspector