Express4+mongodb Super Simple Getting Started example

Source: Internet
Author: User
Tags generator

Before you start, make sure that MongoDB is working, installation Tutorial: Installation and configuration of MongoDB under Windows, install your own configuration. Enter the following text:

First step: command line Create a database, table, and insert a piece of data

The command is as follows:

Create a database Use blog//create a table and insert a data Db.users.insert ({"Name": "Zhangangs", "Emali": "[email protected]", "Age": "26"});// View Data Db.users.find ();

 

Note: MongoDB automatically adds s when creating collections automatically, when manually created, it is recommended to add, (Bo Master just climbed up from this pit ~ ~) using the Mongoose process encountered doubts (has been resolved)

Step Two: Install Express+mongoose (default you have installed Nodejs)

Global installation EXPRESSNPM install express-g//installed Express Global generator, you can automatically generate project files NPM install Express-generator-g

In the current working directory, a project called blog is generated, and the Ejs template is used.

The default is the Jade template,-E is the use of Ejs template Express-e Blog

Then install all dependencies:

Go to project folder CD blog//Install all dependent NPM Install

Startup Project under Windows Platform,

The official method, because it is debug mode, did not delve into set Debug=blog $ npm start//directly start NPM start

Then open the http://localhost:3000/in the viewer to see the application, here, Express is the installation completed.

Installing the Mongoose Module

Enter the current project root directory and enter:

Install Mongoose,   --save add Mongoose module to project dependent NPM install Mongoose--save

The installation complete Package.json file is as follows:

Mongoose Module installation is complete!

Step three: Express Operation MongoDB

1, modify the Route/index.js

var express = require (' Express '); var router = Express. Router ();//mongodbvar Mongoose = require (' Mongoose '); Mongoose. Promise = Global. Promise;        var conn = mongoose.connect (' Mongodb://127.0.0.1:27017/blog '); var User = new Mongoose. Schema ({    name:string,    email:string,    age:string}), var mymodel = Conn.model (' user ', user);/* GET index Listing. */router.get ('/', function (req, res, next) {    Mymodel.findone ({name: "Zhangangs"}, function (err, user) {        Console.log (user);        Res.render (' index ', {title: ' Express ', User:user});})    ; Module.exports = router;

Modify Wiew/index.ejs

<! DOCTYPE html>

Restart the server, refresh the page, the effect is as follows:

Complete, (>_<)

The pro-test is available. Attention:
1. The third step connects the database with the new version:
var conn = Mongoose.connection.openUri (' Mongodb://127.0.0.1:27017/blog ');
2. When connecting to the database, you need to ensure that MongoDB is open

Reprint: https://www.cnblogs.com/zhangans/p/6242898.html

Express4+mongodb Super Simple Getting Started 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.