Micro-channel applet + thinkjs + mongoDB simple interaction front and rear ends

Note: this time with a teacher to learn a little mongodbdatabase, this is the first time to build back-office services, a lot of errors, be hereby re-set it, thank you very much for the help of my classmates -

First, the use thinkjs + mongodb create a background service

1. Install thinkjs

Installation thinkjsKit

npm install -g think-cli

2. Create a project thinkjs

thinkjs new demo;
cd demo;
npm install;
npm start;

3. Let the model framework to support mongo

In the demoproject configfolder extend.jsto add the file think-mongomodule

const mongo = require('think-mongo');

module.exports = [
    mongo(think.app)
]

4. Connect mongodb

installationthink-mongo

npm install think-mongo

Modify configfolder adapter.jsfiles in the database

exports.model = {
    type: 'mongo',
    common: {
        logConnect: isDev,
        logger: msg => think.logger.info(msg)
    },
    mongo: {
        host: '127.0.0.1',
        database: 'mytest',  // 自己创建的数据库名字
        port: 27017,
        user: '',
        password: ''
    }
};

5. Create a database mongodb

In the demoproject root directory new under a dbfolder for storing data , and open the database service.

Note: After the services have opened in this folder, or back-end database can not connect

mkdir db
cd db
mongod --dbpath=./

windowsAs used herein the user powershellterminal, do not use cmdthe command window.

6. Add Route

Modification index Controller

Into the controllerfolder in the indexfolder among the data modify return

module.exports = class extends Base {
    indexAction() {
        return this.json({nihao: '\'nihao\''});
    }
};

7. Add your own controller

Create an arbitrary name of the .jsfile, and then customize the content controller returns

const Base = require('./base.js');

module.exports = class extends Base {
    async indexAction() {
        // 获取从微信小程序传过来的 data 数据
        const data1 = this.post('data');
        // 将获取的数据 data1 添加到 student 表中
        const a = await this.mongo('student').add(data);
        // 从控制台输出 a 的地址
        console.log(a);
        return this.success('success');
    }
    
    async addAction() {
        const test = 'hello, world';
        return this.json({test});
    }
}

8. Add a controller mongodb

In the controllerFolder to create a new user.jsfile, which writes the contents of the controller

const Base = require('./base.js');

module.exports = class extends Base {
    async indexAction() {
        // controller 中实例化模型
        const user = await this.mongo('user').find();
        if (think.isEmpty(user)) {
            return this.fail();
        } else {
            return this.success(user);
        }
    }
};

9. Add model file

Each model for various operations such as reading data here

module.exports = class extends think.Mongo {
    find() {
        return this.model('user').select();
    };
}

Second, the micro-channel configuration applet

Sign up and create micro letter applet not go into here, directly to the theme

1. Add the page load function wx.request

In the micro-channel applet created by index.jsadding the following code files

Page({
    onLoad: function() {
        wx.request({
            // 这里的 test 是上面第7步创建的自己的控制器的名字
            url: 'http://127.0.0.1:8360/test/index'
            method: 'POST',
            data: {
                data: 'hello'
            },
            success: function (res) {
                console.log(res.data)
            }
        })
    }
})

Third, check the front and rear side interaction

1. Turn on the demo service

In demothe project directory

npm start;

2. Open the database mongodb

In the demodirectory of dbopen file folder database services

cd db
mongod --dbpath=./

Creating databases and collections

use database
db.createCollection("collection")

3. Compile small micro-channel program

End

expected results:

Database data appeared applet micro-channel data is added, the data is successfully transmitted to the distal end back into the database;

Micro-channel successfully received applet success message is returned back;

demo project console output variable address string of received data.

At this point, we completed a simple front and rear side interactions friends ~

Guess you like

Origin www.cnblogs.com/xpcloud/p/11409072.html