Express+mongodb Developing Web Backend interface

Source: Internet
Author: User
Tags connect mongo mongodb

Summary:

Express Development Web Interface

Non-relational database MongoDB

Linking and manipulating MongoDB using Nodejs and Mongoose modules

First, Express

Express, Fast, developed, minimalist web development framework based on NODEJS

Install Express:

NPM Install Express--save

The Server.js file is as follows:

Const Express=require (' Express ');//new AppConst app=Express ();//Client Access/When returning a piece of textApp.get ('/',function(req,res) {res.send (' <p>hello world</p> ')})//return JSON data when client accesses/dataApp.get ('/data ',function(req,res) {Res.json ({"Name": "Lizhao", "Age": "2"})})//Monitor 9093App.listen (9093,function() {Console.log (' node app start 9093 ')})

App.get, app.post respectively develop Get,post interface, App.use use module, Res.send, Res.json, res.sendfile respond to different content.

Installing Nodemon

Listens for routing and response content so that the service starts automatically after each modification.

NPM Install Nodemon-g

Start

Nodemon Server.js
Second, non-relational database MongoDB

The installation and use of MongoDB is described in:

Iii. linking and manipulating MongoDB using Nodejs and Mongoose modules

Installing Mongoose

NPM Install Mongoose--save

The JSON is stored by Mongoose Operation Mongodb,mongodb, which is much easier than MySQL.

Basic use of Mongoose

Connect Connection Database

Model new models, schema definition document models

Create,remove,update to delete and change

Find and FindOne to query the data

Const Express=require (' Express '); Const Mongoose=require (' Mongoose ');//connect MONGO, and use IMOOC this collectionConst db_url= ' MONGODB://127.0.0.1:27017/IMOOC '; Mongoose.connect (Db_url); Mongoose.connection.on (' Connected ',function() {Console.log (' Connect ')})//similar to the MySQL table, MONGO has the concept of a document, a field, a new document modelConst User=mongoose.model (' User ',NewMongoose. Schema ({user:{type:string,require:true}, Age:{type:number,require:true}}))//Increaseuser.create ({User:' Lizhao ', Age:19 },function(err,doc) {if(!err) {Console.log (DOC)}Else{Console.log (Err)}})//new AppConst app=Express ();//Client Access/When returning a piece of textApp.get ('/',function(req,res) {res.send (' <p>hello world</p> ')})//JSON data returned when the client accesses/data with the query userApp.get ('/data ',function(req,res) {//CheckUser.find ({},function(err,doc) {if(!err) {      returnRes.json (DOC)});})//DeleteUser.remove ({age:18},function(Err,doc) {Console.log (DOC)})//ChangeUser.update ({age:19},{' $set ': {age:20}},function(Err,doc) {Console.log (DOC)})//Monitor 9093App.listen (9093,function() {Console.log (' node app start 9093 ')})

Express+mongodb Developing Web Backend interface

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.