小程序封装request请求工具

使用示例

统一管理API :

1
2
3
4
const request = require('../utils/request')

// 首页数据
export const getIndexData = (data = {}) => request.get('/api/index', data, false)

在页面对应的js中引用:

1
import {getIndexData} from "../../service/index";
阅读更多

git常用命令

持续更新中…

git init 初始化本地git仓库

git remote add origin 远程仓库地址 把本地仓库和远程仓库关联

git add 文件名 把工作区文件加入到索引区 git add . 把所有文件加入到索引区

git commit 提交到本地仓库 -m “提交信息”

git commit --amend 追加提交

阅读更多

Nodejs + MongoDB模糊搜索

MongoDB中模糊查询要使用正则表达式,使用nodejs和mongodb实现一个通过搜索框对数据库进行搜索的功能,一开始直接用的findOne()方法

1
2
3
Corpus.findOne({name: '/'+req.query.name.replace(/"/g, '')+'/'}, function (err, Corpus) {
...
})

很奇怪,这种方式并查不到东西,把name对应的值写死后,却可以正常查到内容,但是req.query.name是可以正常获取到前台输入的内容的。

解决:

在nodejs中要用RegExp构建正则表达式对象

1
2
3
4
5
6
7
let CorpusSearch = req.query.name.replace(/"/g, '')
let str = ".*"+CorpusSearch+".*$"
let reg = new RegExp(str)

Corpus.find({name:{$regex:reg, $options: 'i'}}, function (err, Corpus) {
...
})
阅读更多

JS模块化,require、import和export

require

遵循AMD规范,在运行时加载,可以将js文件以模块的方式引入。

1
2
3
4
5
// 引入hello模块
const hello = require('./hello');

// 调用hello模块中使用exports暴露的world()方法
hello.world();
阅读更多