-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
baac33d
commit 2d77170
Showing
34 changed files
with
4,818 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* @Description:初始化 | ||
* @Autor: WangYuan | ||
* @Date: 2022-03-02 14:27:50 | ||
* @LastEditors: WangYuan | ||
* @LastEditTime: 2022-03-02 15:08:22 | ||
*/ | ||
const Koa = require('koa') | ||
const app = new Koa() | ||
const views = require('koa-views') | ||
const json = require('koa-json') | ||
const onerror = require('koa-onerror') | ||
const bodyparser = require('koa-bodyparser') | ||
const koaBody = require('koa-body') | ||
const logger = require('koa-logger') | ||
const cors = require('koa2-cors') | ||
const Router = require('koa-router') | ||
const router = new Router() | ||
const registerRouter = require('./routes') | ||
|
||
process.env.NODE_ENV = 'development'; | ||
|
||
// 链接mongodb数据库 | ||
require('./utils/mongodb') | ||
|
||
// error handler | ||
onerror(app) | ||
|
||
// middlewares | ||
app.use( | ||
cors({ | ||
credentials: true, | ||
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'], | ||
allowMethods: ['GET', 'POST', 'DELETE'], | ||
allowHeaders: ['Content-Type', 'Authorization', 'Accept'] | ||
}) | ||
) | ||
|
||
app.use(json()) | ||
app.use(logger()) | ||
app.use( | ||
koaBody({ | ||
formLimit: '15mb', | ||
jsonLimit: '15mb', | ||
textLimit: '15mb' | ||
}) | ||
) | ||
app.use(require('koa-static')(__dirname + '/public')) | ||
|
||
app.use( | ||
views(__dirname + '/views', { | ||
extension: 'pug' | ||
}) | ||
) | ||
|
||
/** | ||
* 错误捕捉中间件 | ||
*/ | ||
app.use(async (ctx, next) => { | ||
try { | ||
ctx.error = (code, message) => { | ||
if (typeof code === 'string') { | ||
message = code | ||
code = 500 | ||
} | ||
ctx.throw(code || 500, message || '服务器错误') | ||
} | ||
await next() | ||
} catch (e) { | ||
let status = e.status || 500 | ||
let message = e.message || '服务器错误' | ||
ctx.response.body = { status, message } | ||
} | ||
}) | ||
|
||
// logger | ||
app.use(async (ctx, next) => { | ||
const start = new Date() | ||
await next() | ||
const ms = new Date() - start | ||
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`) | ||
}) | ||
|
||
// routes | ||
app.use(registerRouter()) | ||
app.use(router.routes()) // 中间件中使用router | ||
|
||
// error-handling | ||
app.on('error', (err, ctx) => { | ||
console.error('server error', err, ctx) | ||
}) | ||
|
||
module.exports = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('demo:server'); | ||
var http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
// app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app.callback()); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === 'string' | ||
? 'Pipe ' + port | ||
: 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* @Description: 配置信息 | ||
* @Autor: WangYuan | ||
* @Date: 2022-02-10 19:20:33 | ||
* @LastEditors: WangYuan | ||
* @LastEditTime: 2022-03-02 15:32:46 | ||
*/ | ||
config = { | ||
appid: 'xxx', // 小程序appId | ||
secret: 'xxx', // 小程序secret | ||
serviceApi: 'xxx', // 服务器地址 | ||
mongodbUrl: 'xxx' // mongodb数据库地址 格式:mongodb://username:password@host:port/name | ||
} | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* @Description: What's this for | ||
* @Autor: WangYuan | ||
* @Date: 2021-08-17 15:33:27 | ||
* @LastEditors: WangYuan | ||
* @LastEditTime: 2021-09-16 20:06:33 | ||
*/ | ||
const mongoose = require('mongoose') | ||
const goodsModel = mongoose.model('goods') | ||
const channel = require('../utils/channel') | ||
|
||
const helper = { | ||
// 根据商城id查询所属商品,筛选掉richText字段 | ||
findAll: (params) => { | ||
return new Promise((resolve, reject) => { | ||
goodsModel.find({ projectId: { $eq: params.projectId }, name: { $regex: params.name } }, { richText: 0 }, (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(data) | ||
} | ||
}) | ||
}) | ||
}, | ||
// 根据商城id和商品id集合查询对应商品,筛选掉richText字段 | ||
findIds: (id) => { | ||
return new Promise((resolve, reject) => { | ||
goodsModel.find({ projectId: { $eq: id } }, { richText: 0 }, (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(data) | ||
} | ||
}) | ||
}) | ||
}, | ||
// 根据id查询详情 | ||
findById: (id) => { | ||
return new Promise((resolve, reject) => { | ||
goodsModel.findById(id, (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
channel.mappingId(data) | ||
resolve(data) | ||
} | ||
}) | ||
}); | ||
}, | ||
// 编辑 | ||
edit: (data) => { | ||
return new Promise((resolve, reject) => { | ||
goodsModel.updateOne({ _id: data.id }, data, (err, d) => { | ||
if (err) { | ||
reject({ message: '编辑失败', status: 10001 }) | ||
} else { | ||
resolve({ message: '编辑成功', status: 10000, id: data.id }) | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = helper |
Oops, something went wrong.