Nodejs+express+mongodb a simple example

Source: Internet
Author: User

OK, let's talk about my purpose of learning node:It's very comfortable to use .Please shave off the black area.

then, I introduced the next node+express+mongodb these three things, I am afraid that some people do not know what this is .

Node
: Is running on the server side of the program language, the surface of the past is the same thing as JavaScript, but, is really 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 manipulate node (because native node is more cumbersome to write, and because node isEvent-Driven, so there are a lot of asynchronous callbacks, write more and look dizzy ... )
MongoDB: This is a non-relational database (NoSQL), too deep things I do not know, anyway, the goods also have a very strong place, the disadvantage is not suitable forDataConsistencyHigh demand, such as financial development. But the advantage iscome on .
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 the installation nodejs,http://www.nodejs.org/, install it and then check whether the path is automatically configured in the system environment variable, If not, please configure node's installation address to 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 Cache directory
Install Express (new version also installs Express-generator)
NPM i-g Express
NPM i-g express-generator
then add the./bin address under the Global module path in the system environment variable.
Test:
Express-v success will show version number
3.mongodb: This database installation is simple, 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
, you will find that your computer's service is more than one MongoDB Service, yes, that's it, and then you run the service on the line.

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 meeting user component var Schema = Mongoose. schema;//Create model var userschema = new Schema ({userid:string,password:string});//define a new model, However, this pattern has not been associated with the Users collection Exports.user = Mongoose.model (' users ', Userschema); Associating 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.

3Index.js configuration route under routes, that is, request mapping processing

Index.js


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.


Nodejs+express+mongodb a simple example

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.