Express+mongodb开发web后台接口

  • Express开发web接口
    Express是基于nodejs,快速、开放、极简的web开发框架
    // 安装express
    npm install express--save
    // server.js
    const express = require('express')
    
    // 新建app
    const app = express()
    
    app.get('/', function(req, res){
      res.send('<h1>Hello world</h1>')
    })
    
    app.listen(3000, function(){
      console.log('Node app start at port 3000')
    })
    
    // 此时在server.js目录下命令行运行node server.js
    // http://localhost:3000渲染出Hello world
    // 修改渲染信息
    // server.js
    const express = require('express')
    
    // 新建app
    const app = express()
    
    app.get('/', function(req, res){
      res.send('<h1>Hello world</h1>')
    })
    
    app.get('/data', function(req, res){
      res.json({user: 'Edison', age: 20})
    })
    
    app.listen(3000, function(){
      console.log('Node app start at port 3000')
    })
    
    // http://localhost:3000/data 取不到对象

    此时需要在命令行重新运行node server.js,为了不重复操作,安装nodemon:

    // 安装nodemon
    npm install nodemon--save

    在server.js文件同级目录命令行中:nodemon server.js,此后修改数据自动更新localhost:3000
     

  • 非关系型数据库mongodb
    安装mongodb,管理员身份运行cmd命令行,在mongodb\bin目录下运行mongo,出现类似如下代码为运行成功
    F:\>cd F:\workTool\mongodb\bin
    
    F:\workTool\mongodb\bin>mongo
    MongoDB shell version v4.0.5
    connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("df58d557-2e9f-4508-a1db-98cdfb83847d") }
    MongoDB server version: 4.0.5
    Server has startup warnings:
    2019-01-21T09:05:01.710+0800 I CONTROL  [initandlisten]
    2019-01-21T09:05:01.710+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    2019-01-21T09:05:01.710+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    2019-01-21T09:05:01.710+0800 I CONTROL  [initandlisten]
    ---
    Enable MongoDB's free cloud-based monitoring service, which will then receive and display
    metrics about your deployment (disk utilization, CPU, operation statistics, etc).
    
    The monitoring data will be available on a MongoDB website with a unique URL accessible to you
    and anyone you share the URL with. MongoDB may use this information to make product
    improvements and to suggest MongoDB products and deployment options to you.
    
    To enable free monitoring, run the following command: db.enableFreeMonitoring()
    To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

     

  • 使用nodejs的mongoose模块链接和操作mongodb
    链接mongo,并使用Edison这个集合:
    const DB_URL = 'mongodb://127.0.0.1:27017/Edison'
    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: 'Edison',
      age: 20
    }, function(err, doc){
      if (!err) {
        console.log(doc)
      } else {
        console.log(err)
      }
    })
    查找数据:
    app.get('/data', function(req, res){
      User.find({}, function(err, doc){
        res.json(doc)
      })
    })
    
    // 只查一条
    app.get('/data', function(req, res){
      User.findOne({}, function(err, doc){
        res.json(doc)
      })
    })
    删除数据:
    User.remove({age: 20}, function(err, doc){
      if (!err) {
        console.log(doc)
      } else {
        console.log(err)
      }
    })
    修改数据:
    // 更新数据
    User.update({'user': 'Edison'}, {'$set': {age: 26}}, function(err, doc){
      console.log(doc)
    })



     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值