1.官网下载(最新版的是自带图形可视化工具的)Mongodb
2.安装完最新版本桌面会有MongoDBCompass的图标,打开应用,直接点击connect即可
前往目录mall-cook/mall-cook-service/
, 打开根目录的config.js
config = {
appid: 'xxx', // 小程序appId
secret: 'xxx', // 小程序secret
serviceApi: 'http://127.0.0.1:3000', // 服务器地址 用于上传完图片后拼接 详细看routes/upload.js
mongodbUrl: 'mongodb://localhost:27017/mall-cook' // mongodb数据库地址 格式:mongodb://username:password@host:port/name
}
module.exports = config
# 如果没有在mall-cook-service安装依赖 先yarn install 安装依赖
yarn dev
# 启动后端口时koa默认端口3000
:::tip 由于涉及线上部署问题,这个问题需要手动修改 :::
- 安装dayjs
yarn add dayjs
- 目录处理
// 额外引入这几个模块
const path = require('path')
const fs = require('fs')
const dayjs = require('dayjs')
// routes/upload.js
// 19行
destination: function (req, file, cb) {
// appjs中koa-static配置目录为public,因此上传目录放public; 也可以配置koa-static
const filePath = `${path.resolve('./public')}/img/${dayjs(Date.now()).format('YYYYMMDD')}`
// 判断目录是否存在,不存在自动创建
fs.access(filePath, (err) => {
if(!err) {
cb(null, filePath)
} else {
fs.mkdirSync(filePath)
cb(null, filePath)
}
})
}
// 35行
data: `${config.serviceApi}/img/${dayjs(Date.now()).format('YYYYMMDD')}/${ctx.req.file.filename}`
- 这里用的端口是koa的默认端口3000,因此前端项目的请求地址是 http://127.0.0.1:3000/
// 前端项目 mall-cook-template, 修改utils/request.js
const baseUrl = 'http://127.0.0.1:3000' // 将请求地址改为本地的nod服务
// 后台管理端 mall-cook-platform 修改config/global.js
export default {
baseApi: 'http://127.0.0.1:3000/',
viewUrl: 'http://127.0.0.1:8081/#/' // 这个的端口不一定是8081 看你mall-cook-template启动时的端口
}