Note: this time with a teacher to learn a little
mongodb
database, 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 thinkjs
Kit
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 demo
project config
folder extend.js
to add the file think-mongo
module
const mongo = require('think-mongo');
module.exports = [
mongo(think.app)
]
4. Connect mongodb
installationthink-mongo
npm install think-mongo
Modify config
folder adapter.js
files 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 demo
project root directory new under a db
folder 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=./
windows
As used herein the user powershell
terminal, do not use cmd
the command window.
6. Add Route
Modification index Controller
Into the controller
folder in the index
folder 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 .js
file, 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 controller
Folder to create a new user.js
file, 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.js
adding 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 demo
the project directory
npm start;
2. Open the database mongodb
In the demo
directory of db
open 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 ~