v0.0.3
This commit is contained in:
23
src/app.js
23
src/app.js
@ -1,32 +1,23 @@
|
||||
import Koa2 from 'koa'
|
||||
import KoaBodyParser from 'koa-bodyparser'
|
||||
import KoaBody from 'koa-body'
|
||||
import KoaSession from 'koa-session2'
|
||||
import KoaStatic from 'koa-static2'
|
||||
import { SystemConfig } from '../config/main.js'
|
||||
import path from 'path'
|
||||
|
||||
import MainRoutes from './routes/main-routes'
|
||||
import ErrorRoutes from './routes/error-routes'
|
||||
import PluginLoader from './tool/PluginLoader'
|
||||
// import PluginLoader from './tool/PluginLoader'
|
||||
|
||||
const app = new Koa2()
|
||||
const BodyParser = new KoaBodyParser()
|
||||
const env = process.env.NODE_ENV || 'development' // Current mode
|
||||
|
||||
app.use(BodyParser({
|
||||
detectJSON: function (ctx) {
|
||||
return /\.json$/i.test(ctx.path)
|
||||
},
|
||||
extendTypes: {
|
||||
json: ['application/x-javascript'] // will parse application/x-javascript type body as a JSON string
|
||||
},
|
||||
onerror: function (err, ctx) {
|
||||
ctx.throw('body parse error:' + err, 422)
|
||||
}
|
||||
app.use(KoaBody({
|
||||
multipart: true
|
||||
// formidable: {uploadDir: path.join(__dirname, '../../../assets/uploads/')}
|
||||
})) // Processing request
|
||||
.use(KoaStatic('assets', path.resolve(__dirname, '../assets'))) // Static resource
|
||||
.use(KoaSession({key: 'RESTfulAPI'})) // Set Session 生产环境务必随机设置一个值
|
||||
.use(PluginLoader(SystemConfig.System_plugin_path))
|
||||
.use(KoaSession({key: SystemConfig.Session_Key})) // Set Session
|
||||
// .use(PluginLoader(SystemConfig.System_plugin_path))
|
||||
.use((ctx, next) => {
|
||||
if (ctx.request.header.host.split(':')[0] === 'api.XXX.com' || ctx.request.header.host.split(':')[0] === '127.0.0.1') {
|
||||
ctx.set('Access-Control-Allow-Origin', '*')
|
||||
|
@ -0,0 +1,30 @@
|
||||
export let Get = (ctx) => {
|
||||
ctx.body = {
|
||||
result: 'get',
|
||||
name: ctx.params.name
|
||||
}
|
||||
}
|
||||
|
||||
export let Post = (ctx) => {
|
||||
ctx.body = {
|
||||
result: 'post',
|
||||
name: ctx.params.name,
|
||||
para: ctx.request.body.para
|
||||
}
|
||||
}
|
||||
|
||||
export let Put = (ctx) => {
|
||||
ctx.body = {
|
||||
result: 'put',
|
||||
name: ctx.params.name,
|
||||
para: ctx.request.body.para
|
||||
}
|
||||
}
|
||||
|
||||
export let Delect = (ctx) => {
|
||||
ctx.body = {
|
||||
result: 'delect',
|
||||
name: ctx.params.name,
|
||||
para: ctx.request.body.para
|
||||
}
|
||||
}
|
||||
|
35
src/controllers/upload.js
Normal file
35
src/controllers/upload.js
Normal file
@ -0,0 +1,35 @@
|
||||
import multer from 'koa-multer'
|
||||
import path from 'path'
|
||||
|
||||
/***
|
||||
* 说明:该方法只支持:`multipart/form-data`方式的上传。postman可以调试。
|
||||
*/
|
||||
|
||||
let storage = multer.diskStorage({
|
||||
// 设置上传后文件路径
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, path.join(__dirname, '../../../assets/uploads/'))
|
||||
},
|
||||
// 给上传文件重命名,获取添加后缀名
|
||||
filename: function (req, file, cb) {
|
||||
let fileFormat = (file.originalname).split('.')
|
||||
cb(null, Date.now() + '.' + fileFormat[fileFormat.length - 1])
|
||||
}
|
||||
})
|
||||
|
||||
export let upload = multer({
|
||||
storage: storage,
|
||||
// dest: 'uploads/', //上传文件保存的路径,如果你想在上传时进行更多的控制, 你可以使用storage选项替代dest. Multer 具有 DiskStorage 和 MemoryStorage 两个存储引擎; 另外还可以从第三方获得更多可用的引擎.
|
||||
// fileFilter: function (req, file, cb) {
|
||||
// let mimetypes = (['text/*', 'image/*', 'video/*', 'audio/*', 'application/zip']).join(',');
|
||||
// let testItems = file.mimetype.split('/');
|
||||
// if ((new RegExp('\b' + testItems[0] + '/\*', 'i')).test(mimetypes) || (new RegExp('\*/' + testItems[1] + '\b', 'i')).test(mimetypes) || (new RegExp('\b' + testItems[0] + '/' + testItems[1] + '\b', 'i')).test(mimetypes)) {
|
||||
// cb(null, true);
|
||||
// } else {
|
||||
// return cb(new Error('Only image, plain text, audio, video and zip format files are allowed!'), false);
|
||||
// }
|
||||
// }, // fileFilter要在这里声明才行,用instance.fileFilter = funciton(){};是不管用的,
|
||||
limits: {
|
||||
fileSize: 200 * 1024 * 1024 // Max file size in bytes (20 MB) 限制上传的文件大小,不设置则是无限
|
||||
}
|
||||
})
|
12
src/lib/sequelize.js
Normal file
12
src/lib/sequelize.js
Normal file
@ -0,0 +1,12 @@
|
||||
import Sequelize from 'sequelize'
|
||||
import { SystemConfig } from '../config/main'
|
||||
|
||||
export default new Sequelize(SystemConfig.mysql_database, SystemConfig.mysql_user, SystemConfig.mysql_password, {
|
||||
host: SystemConfig.mysql_host,
|
||||
dialect: 'mysql',
|
||||
pool: {
|
||||
max: 50,
|
||||
min: 0,
|
||||
idle: 10000
|
||||
}
|
||||
})
|
@ -1,4 +1,5 @@
|
||||
import nodemailer from 'nodemailer'
|
||||
import { SendEmail } from '../../../config/main'
|
||||
|
||||
// 发送Email(目前使用的是阿里云SMTP发送邮件)
|
||||
// receivers 目标邮箱,可以用英文逗号分隔多个。(我没试过)
|
||||
@ -10,11 +11,11 @@ import nodemailer from 'nodemailer'
|
||||
// info 是返回的消息,可能是结果的文本,也可能是对象。(这个错误不要暴露给用户)
|
||||
export let sendemail = (receivers, subject, text, html) => {
|
||||
return new Promise(function (resolve) {
|
||||
let transporter = nodemailer.createTransport('smtp://postmaster%40abcd.com:password@smtp.abcd.com')
|
||||
let transporter = nodemailer.createTransport('smtp://' + SendEmail.username + ':' + SendEmail.password + '@' + SendEmail.service)
|
||||
|
||||
// setup e-mail data with unicode symbols
|
||||
let mailOptions = {
|
||||
from: '"XX平台 👥" <postmaster@abcd.com>', // sender address
|
||||
from: SendEmail.sender_address, // sender address
|
||||
to: receivers,
|
||||
subject: subject,
|
||||
text: text || 'Hello world 🐴', // plaintext body
|
@ -1,5 +1,5 @@
|
||||
import KoaRouter from 'koa-router'
|
||||
// import controllers from '../controllers/index.js'
|
||||
import controllers from '../controllers/index.js'
|
||||
|
||||
const router = new KoaRouter()
|
||||
|
||||
@ -7,9 +7,17 @@ router
|
||||
.get('/', function (ctx, next) {
|
||||
ctx.body = '禁止访问!'
|
||||
}) // HOME 路由
|
||||
// .get('/api/:api_type/:name', controllers.api.api_get)
|
||||
// .put('/api/:api_type/:name', controllers.api_put.api_put
|
||||
// .post('/api/:api_type/:name', controllers.api.default)
|
||||
// .delect('/api/:api_type/:name', controllers.api.default)
|
||||
.all('/upload', controllers.upload.upload.array('file'), function (ctx, next) { // 上传到本地示例
|
||||
return new Promise(function (resolve, reject) {
|
||||
// 允许跨域,正式环境要改为对应域名
|
||||
ctx.set('Access-Control-Allow-Origin', '*')
|
||||
// ctx.req.files.filename = ctx.req.files.path
|
||||
resolve(ctx.body = ctx.req.files)
|
||||
})
|
||||
})
|
||||
.get('/api/:name', controllers.api.Get)
|
||||
.post('/api/:name', controllers.api.Post)
|
||||
.put('/api/:name', controllers.api.Put)
|
||||
.del('/api/:name', controllers.api.Delect)
|
||||
|
||||
module.exports = router
|
||||
|
@ -34,8 +34,8 @@ export let OptionFormat = (GetOptions) => {
|
||||
|
||||
// 替换SQL字符串中的前缀
|
||||
export let SqlFormat = (str) => {
|
||||
if (SystemConfig.mysql_prefix !== 'bm_') {
|
||||
str = str.replace(/bm_/g, SystemConfig.mysql_prefix)
|
||||
if (SystemConfig.mysql_prefix !== 'api_') {
|
||||
str = str.replace(/api_/g, SystemConfig.mysql_prefix)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
Reference in New Issue
Block a user