Node+express+mongodb First Experience

Source: Internet
Author: User
Tags install mongodb mongodb documentation object model

From last November to now, has been want to learn nodejs, in this time experience Gulp, grunt, Yeomen,fis, but for Nodejs in-depth to learn, to develop projects are always intermittent.

Today spent a day to learn to organize the whole learning ideas, the following is my learning to share, is the entry-level learning experience for NODE+MONGODB development of small white, node has played a long time the great God this article may not be suitable.

Here's an example:

 Client Form page:

<! DOCTYPE html>"en -US">"UTF-8"> <title> Form pages </title>"http://localhost:3000/"> <input type="text"Name="title"/> <textarea name="text"Id=""cols=" -"Rows="Ten"></textarea> <input type="Submit"/> </form></body>

  Server's page:

varHTTP = require ("http");varOpen = require ("child_process");varQueryString = require ("QueryString");varServer =http.createserver (function (req,res) {varPost ="'; Req.on ('Data', function (chunk) {post+=Chunk;        }); Req.on ('End', function () {post=Querystring.parse (POST); Res.writehead ( $,{"Content-type":"Text/html;charset=utf-8"});        Res.write (Post.title);        Res.write (Post.text);    Res.end (); });}). Listen ( the,'127.0.0.1'); Open.exec ("Start http://127.0.0.1:3000");

In this simple example, we implement the foreground form submission and retrieve the data in the background.

With this simple example, we might want to do something more complicated, so how do we do it, Node+express+mongodb:

  The first part, basic knowledge point Summary

express--a simple and flexible node. JS Web application Framework

mongodb--a relational database for node

Mongoose-Mongoose is a MongoDB object Model tool that is similar to ORM, making Nodejs easier to manipulate MongoDB database, Mongoose document

A deep discussion on node template engine--ejs,jade node template engine

Web templates Jade, EJS, handlebars code interpretation efficiency comparison, Jade complete defeat HTTPS://CNODEJS.ORG/TOPIC/50E70EDFA7E6C6171A1D70FA

EJS--http://www.embeddedjs.com/github Documentation--Https://github.com/tj/ejs

jade--HTML template Engine from node. JS http://segmentfault.com/a/1190000000357534

JADEAPI---Http://www.nodeclass.com/api/jade.html#doctype

MongoDB---http://docs.mongodb.org/manual/manual

MongoDB Introduction and Installation--http://www.cnblogs.com/lipan/archive/2011/03/08/1966463.html

What is MongoDB? ---http://www.w3cschool.cc/mongodb/mongodb-intro.html

  Part II, configuring the development installation package

   ① Configuration Package.json---NPM init initial configuration package dependencies

{  "name":"Pfan",  "version":"1.0.1",  "Description":"Pfan",  "Main":"App.js",  "Dependencies": {    "Express":"^4.12.4",    "Jade":"^1.10.0",    "Mongoose":"^4.0.3"  },  "devdependencies": {    "Body-parser":"^1.12.4"  },  "Scripts": {    "Test":"echo \ "Error:no test specified\" && exit 1"  },  "author":"",  "License":"ISC"}

  Note here: In fact, it is very important to configure Package.json, we write the required dependent modules, and then through the NPM install can be installed in the parameters of the dependencies, here, in fact, we can install NPM Installation Express Generator Build the directory structure of the node project and the Package.json of the dependent modules 

② quickly build a Web MVC directory structure

Express is a lightweight, concise and easy-to-use node. JS WEB MVC Development Framework
Global installation, you can quickly build a directory structure Web MVC directory structure

NPM install-g Express-generator

Create an express WEBMVC directory structure project

Express DevExpress

③ Installing the Node module package relies on NPM install Express

NPM Install Express//Web application FrameworkNPM Install Jade//Template engineNPM Install Ejs//Template engineNPM Install Mongoose//MongoDB is an object model tool that is easier to operateNPM Install MongoDB//MongoDB's DriveNPM Install Serve-favicon//Unified Settings Site iconNPM Install Morgan//HTTP Request Log MiddlewareNPM Install Cookie-parser//Cookie ParserNPM Install Body-parser//Defining data parsersNPM Install Bower-G Bower Install bootstrap

    Note: One problem is that we execute the NPM install in the command window to execute the package dependency in Package.json, then we execute it through the Express project name, then we can execute the dependency in the project case through the NPM install. thereby installing the entire project framework

   ⑤ Start Express Project

NPM start    // looks like the current project has turned into this.

Part III, MONGODB pits---mongodb documentation (some old APIs to make changes)

1. Installation

First, the installation of MongoDB, directly decompression installation, here no longer repeat.

Next, let's talk about MongoDB and node, and create a webapp new file data to store the data in the project folder.

CD node/Webappmkdir/   /New data file for storing

This result shows that the connection to MongoDB was successful!

2. Create a database

Enter MongoDB installation directory D:\Program files\mongodb\bin, knock command MONGO, switch database WebApp

CD  D:\Program files\mongodb\bin  // Enter directory MONGO           // execute MONGO  use WebApp    // Use WEBAPP database, switch database

Insert a usercollection table to execute:

" username " " TestUser1 " " Email " " [email protected] " })

Execute, view inserted data, whether successful

Db.usercollection.find (). Pretty ()   // View inserted data, whether successful

3. Configure the user name and password for the database

CD D:\Program Files\mongodb\bin//Go to CatalogMongo//Executive MONGOUse WebApp//Use the WebApp database to switch the databaseDb.createuser ({createUser:"Test"Pwd:"Test", Roles:[{role:"Dbowner"Db:"WebApp"}]})//set user name password, old version with Db.adduser noteShow Users//WebApp now existing users

    

Last resource sharing:

Express Example: https://github.com/pingfanren/express

Examples of Express and database: Https://github.com/pingfanren/expressSimpleBlog

Blog: http://www.cnblogs.com/myzhibie/

Express Example: Https://github.com/nswbmw/N-blog/wiki/_pages

Leaves-Chai Blog: http://www.cnblogs.com/yexiaochai/p/3527418.html

Node+express+mongodb First Experience

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.