zoukankan      html  css  js  c++  java
  • node.js服务器+mongodb数据库(重拾)

    1、node-express启动服务器

    node app

    127.0.0.1:3000  打开浏览器ok的话就启动成功(在public文件夹放个index.html)

    加上了es6和热更新,npm start

    package.json

    "scripts": {
        "test": "echo "Error: no test specified" && exit 1",
        "start": "nodemon app.js --exec babel-node --presets env"
      },
    "dependencies": {
        "express": "^4.16.2"
      },
      "devDependencies": {
        "babel-cli": "^6.26.0",
        "babel-preset-env": "^1.6.1",
        "babel-preset-es2015": "^6.24.1",
        "babel-preset-stage-2": "^6.24.1",
        "nodemon": "^1.12.1"
      },

    2、接口测试

    前端

              var params={name:'hhl',psd:'123456'};
              let out=Axios.get('/login',{params:params});
              out.then((res) => {
                //console.log(res);
                console.log(res.data);// {name: "hhl", psd: "123456"}
              })

    后端

    app.js

    app.use('/login',require('./routes/login'));

    routes

    router.get('/', function(req, res) {
      res.json(req.query);
    });

    get方法用query,post方法用body,新手要注意还其他设置let bodyParser = require('body-parser');等

    3、启动数据库服务器

     教程见http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html

    mongodb安装后,网上下载比较麻烦,稍后放个地址

    cmd==> bin目录下,mongod --dbpath=(数据存放的目录)

    回车后,有27012,浏览器打开ok

    操作数据库,查看bin目录下mongo

    4、node.js与服务器相连接

    npm安装mongodb

    这边很奇怪了,跟之前的.open()  .collection() 方法都无效了

    原来是版本问题,3.0上版本不知为啥不行,好像是针对其他系统的吧,我用的windows系统,然后看了官网的快速https://www.npmjs.com/package/mongodb

    一下ok

    "dependencies": {
        "express": "^4.16.2",
        "mongodb": "^2.0.55"
      },

    这边我取消了用open,collection用了

    db.js

    //var mongo = require('mongodb');
    //var host = 'localhost';
    //var port = 27017;
    //
    //var server = new mongo.Server(host,port,{auto_reconnect:true});
    //var db = new mongo.Db('ten',server,{safe:true});
    //module.exports = db;
    
    var MongoClient = require('mongodb').MongoClient;
    var DB_CONN_STR = 'mongodb://localhost:27017/ten'; //# 数据库为 ten
    exports.mongo = MongoClient;
    exports.ten = DB_CONN_STR;

    node与mongodb连接

    let Db=require('.././database/db.js');
    let express = require('express');
    let router = express.Router();
    
    router.get('/', function(req, res) {
      let response;
      Db.mongo.connect(Db.ten, function(err,db){
        console.log('链接成功');
        let Users=db.collection('person');
        Users.find({name:'jack'}).toArray(function(err,doc){
          if(doc.length){
            response={'suc':true,'res':doc[0],msg:'成功'};
          }else{
            response={'suc':false,'msg':'账号或密码有误'};
          }
          db.close();
          res.json(response);
        });
        //res.json(response); //此处不行
      });
    });

    上为返回的内容

    5、自己查看数据库

    mongo

    use ten

    show collections 查看表

    show dbs 查看数据库

    db.user.remove({}) 删除集合user

    db.user.count() 集合user内有多少个数据

  • 相关阅读:
    C语言字符串读入函数笔记
    济大路痴
    Super Jumping! Jumping! Jumping!
    SpringMVC中静态资源的处理
    SpringMVC的拦截器讲解
    九、Spring中使用@Value和@PropertySource为属性赋值
    spring中最重要的一些Aware接口
    八、spring生命周期之BeanPostProcessor
    七、spring生命周期之初始化和销毁方法
    六、spring之通过FactoryBean为ioc容器中添加组件
  • 原文地址:https://www.cnblogs.com/hhweb/p/8038790.html
Copyright © 2011-2022 走看看