前言:最近学习vue和react的高阶项目,都需要和Nodejs+Express+MongoDB结合实现全栈开发。这里结合实例Demo和所学项目集中总结一下这部分服务端的基础知识。


一、Express+mongodb开发web后台接口Demo

Express开发web接口

  • Express:基于nodejs,快速、开放、极简的web开发框架  【官网
  • 安装express

    npm install express --save
    
  • 新建server目录:server.js编写后端代码
    const express = require('express');
    
    //新建app模块
    const app = express() app.get('/', function(req, res){
    res.send('<h1>Hello world</h1>') //发送Html
    }) app.get('/data', function(req, res){
    res.json({name:'imooc React App',type:'IT'}) //发送json
    }) app.listen(9093, function(){ //自定义监听端口
    console.log('Node app start at port 9093')
    })

  • 查看调试页面:浏览器访问localhost:9093(每次更新server配置后必须重新启动)

  • 监听路由和响应内容,使用nodemon自动重启

    npm install -g nodemon

    启动应用: nodemon server.js 

  • 其它特性
  1. app.get、app.post分别开发get和post接口
  2. app.use使用模块
  3. res.send、res.json、res.sendfile响应不同的内容(分别返回Html文本、JSON、文件)

    Mongodb:非关系型数据库

  • 启动服务:net start MongoDB
  • 关闭服务:net stop MongoDB
  • 安装mongoose

    npm install mongoose --save
  • 通过mongoose操作mongodb存储的就是json,相当于mysql来说,要容易的多  

    Mongoose基础使用

  • Connect连接数据库

    const mongoose = require('mongoose')
    //链接mongo 并且使用react这个集合
    const DB_URL = 'mongodb://127.0.0.1:27017/imooc' //前提:开启Mongodb服务
    mongoose.connect(DB_URL)
    mongoose.connection.on('connected', function(){
    console.log('mongo connect success')
    })
  • 定义文档模型,Schema和model新建模型
  • Mongoose重要概念
  1. Schema: 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力
  2. Model: 由Schema发布生成的模型,具有抽象属性和行为的数据库操作对
  3. Entity: 由Model创建的实体,它的操作也会影响数据库
  • Schema→Model→Entity的关系
  1. Schema生成Model
  2. Model创造Entity
  3. Model和Entity都可对数据库操作造成影响,但Model比Entity更具操作性

    Mongoose文档类型

  • String, Number等数据结构

    //类似于mysql的表 mongo里有文档、字段的概念
    const User = mongoose.model('user', new mongoose.Schema({
    user: {type:String, require:true},
    age: {type:Number, require:true}
    }))
  • 定create、remove、update分别用来增、删、改的操作
  1. 新增数据

    //新增数据
    User.create({
    user: 'xiaohong',
    age: 10
    }, function(err, doc){
    if(!err){
    console.log(doc)
    }else{
    console.log(err)
    }
    }) app.get('/data', function(req, res){
    //查询User全部数据
    User.find({}, function(err, doc){
    res.json(doc)
    })
    })

      

  2. 删除数据
    //删除数据
    User.remove({age:10},function(err, doc){
    console.log(doc)
    })

  3. 更新数据
    //更新数据
    User.update({'user':'xiaoming'},{'$set':{age: 26}}, function(err, doc){
    console.log(doc)
    })

  • find和findOne用来查询数据
  1. find:查找到的是数组,其中可包含多条数据对象

    //查询数据
    User.find({age:18}, function(err, doc){
    res.json(doc)
    })

  2. findOne:查找到的是数据对象本身

    //查询数据
    User.findOne({user:'xiaoming'}, function(err, doc){
    res.json(doc)
    })

    Demo实例代码

/**
* express Demo
*/
const express = require('express');
const mongoose = require('mongoose') //链接mongo 并且使用react这个集合
const DB_URL = 'mongodb://127.0.0.1:27017/imooc'
mongoose.connect(DB_URL)
mongoose.connection.on('connected', function(){
console.log('mongo connect success')
}) //类似于mysql的表 mongo里有文档、字段的概念
const User = mongoose.model('user', new mongoose.Schema({
user: {type:String, require:true},
age: {type:Number, require:true}
})) // 新增数据
// User.create({
// user: 'xiaolan',
// age: 18
// }, function(err, doc){
// if(!err){
// console.log(doc)
// }else{
// console.log(err)
// }
// }) //删除数据
// User.remove({user: 'xiaolan'},function(err, doc){
// console.log(doc)
// }) //更新数据
// User.update({'user':'xiaoming'},{'$set':{age: 26}}, function(err, doc){
// console.log(doc)
// }) //新建app模块
const app = express() app.get('/', function(req, res){
res.send('<h1>Hello world</h1>') //发送Html
}) app.get('/data', function(req, res){
//查询数据
User.find({}, function(err, doc){
res.json(doc)
})
// res.json({name:'imooc React App',type:'IT'}) //发送json
}) app.listen(9093, function(){
console.log('Node app start at port 9093')
})

二、Express和mongodb结合  

1、mongodb独立工具函数

2、express使用body-parser支持post参数

3、使用cookie-parser存储登录信息cookie


注:转载请注明出处

【重点突破】—— Nodejs+Express+MongoDB的使用基础的更多相关文章

  1. NodeJS+Express+MongoDB

    一.MongoDB MongoDB是开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序丰富:高伸缩性:MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言 ...

  2. nodejs+express+mongodb实现登录注册

    nodejs+express+mongodb实现登录注册 1 简介 登录注册功能使用nodejs+express+mongodb完成,其中对mongodb的操作使用mongoose完成,对mongod ...

  3. NodeJS+Express+MongoDB 简单实现数据录入及回显展示【适合新人刚接触学习】

    近期在看NodeJS相关 不得不说NodeJS+Express 进行网站开发是很不错,对于喜欢玩JS的来说真是很好的一种Web开发组合 在接触NodeJS时受平时Java或者C#中API接口等开发的思 ...

  4. nodejs+express+mongodb简单的例子

    简单的介绍下node+express+mongodb这三个东西.node:是运行在服务器端的程序语言,表面上看过去就是javascript一样的东西,但是呢,确实就是服务器语言,个人觉得在一定层次上比 ...

  5. 从无到有,用Nodejs+express+mongodb搭建简易登陆系统

    前端处理server表示很蛋疼,初学Node,虽然感觉异常强大,但是学起来还是有些吃力的,Node是工具,它不是万能的,搭建一个系统还是需要借助其他一些工具,对于我这个没怎么接触server的前端来说 ...

  6. Nodejs&express+mongodb完成简单用户登录(即Nodejs入门)

    刚了解nodejs,发现nodejs配置起来不复杂,但也有很多需要注意的地方,今天就记录一下,以后也可拿出来看看. 要完成这个简单的示例,从零开始,走三步就行了. 一.搭建开发环境 二.创建项目(ex ...

  7. nodeJs express mongodb 建站(mac 版)

    基本环境 homebrew.node.npm.express.mongodb 1.node .npm : (1)辅助工具:homebrew安装(mac下一个软件管理工具,相当于Red hat的yum, ...

  8. nodeJs express mongodb 建站(window 10 版)

    一.环境搭建 安装 node.git.npm.express.mongodb.主要介绍express.mongodb 的安装. (1)node安装:https://nodejs.org/en/down ...

  9. nodejs+express+mongodb 快速接口开发

    nodejs+mongodb+express API快速生成 使用说明 安装 $ npm install duzq-quick-mongo 建立mongodb数据模型 const mongoose = ...

随机推荐

  1. WireShark:TCP三次握手 抓包

    本机ip:192.168.201.200 服务器ip:192.168.230.20 抓到的数据如下: 第一次握手: SYN标记位为1,表示这是一个连接请求.seq 用于服务端返回确认信息,此时ack ...

  2. webdriver操作iframe标记的编辑器

    1.iFrame有ID 或者 name的情况//进入id="frame1"的frame中,定位id="div1"的div和id="input1&quo ...

  3. ping(NOIP模拟赛Round 4)第一次程序Rank 1!撒花庆祝!~\(≧▽≦)/~

    题目: 恩,就是裸的字符串处理啦. 连标程都打的是暴力(随机数据太水啦!吐槽.) 本来O(n^2q)TLE好吧.. 然后我发明了一种神奇的算法,随机数据跑的很快!,当然最坏复杂度跟标程一样啦. 不过期 ...

  4. 软中断网卡处理&Linux高性能外部设备处理机制&SMP

    转载:http://blog.csdn.net/freas_1990/article/details/9238183 看了一些linux网卡驱动的处理技术,对有些概念还是无法理解,突然搜到这篇文章,挺 ...

  5. OutputDebugString方便格式化WIN32封装

    void TRACE(LPCTSTR lpszFmt, ...) { va_list args; va_start(args, lpszFmt); ; TCHAR *lpszBuf = (TCHAR* ...

  6. kylin

    Kylin只能导入扁平化的Hive表,简而言之,其不支持Hive的复杂数据类型,如array.struct.map等

  7. Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)

    Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't wa ...

  8. POJ 1044: Date bugs

    题目描述 There are rumors that there are a lot of computers having a problem with the year 2000. As they ...

  9. Super Ugly Number -- LeetCode

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  10. ARC 098 D - Xor Sum 2

    Problem Statement There is an integer sequence A of length N. Find the number of the pairs of intege ...