Skip to content
This repository has been archived by the owner on Feb 12, 2019. It is now read-only.

Commit

Permalink
add file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Lxxyx committed Jun 17, 2016
1 parent be3220f commit 6603fb5
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 20 deletions.
7 changes: 2 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
module.exports = {
"extends": "vue",
"env": {
"browser": true,
"node": true,
"es6": true
},
"parser": "babel-eslint",
"rules": {
"semi": [2, "never"],
"quotes": [2, "single"]
}
"parser": "babel-eslint"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ build/Release
# Dependency directories
node_modules
jspm_packages
upload

# Optional npm cache directory
.npm
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"fs-extra": "^0.30.0"
},
"devDependencies": {
"babel-eslint": "^6.0.4",
"eslint": "^2.12.0",
"babel-eslint": "^6.0.4"
"eslint-config-vue": "^1.0.3",
"eslint-plugin-vue": "^0.1.1"
}
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ koa2
5. 纯ES6/7编写,使用koa2的Async/Await,避免回调地狱
6. 自定义错误,可附加错误信息
7. 默认支持gzip,减少传输体积,加快传输速度
8. 开箱即用,无需折腾
8. 支持文件上传
9. 开箱即用,无需折腾

## 启动
直接运行run.js即可
Expand Down
2 changes: 1 addition & 1 deletion template/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"regenerator": true
}]
]
}
}
6 changes: 4 additions & 2 deletions template/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import serve from 'koa-static'
import logger from 'koa-logger'
import convert from 'koa-convert'
import bodyParser from 'koa-bodyparser'
import path from 'path'

import index from './router/index'
import api from './router/api'
Expand All @@ -16,6 +17,7 @@ import test from './router/test'
import { KoaErr } from './helper'

const app = new Koa()

// 全局错误处理
app.use(async (ctx, next) => {
try {
Expand Down Expand Up @@ -57,12 +59,12 @@ app.use(convert(json()))
app.use(bodyParser())

// 设置渲染引擎
app.use(views(__dirname + '/views', {
app.use(views(path.resolve(__dirname, 'views'), {
extension: 'ejs'
}))

// 静态文件夹
app.use(convert(serve(__dirname + '/static/')))
app.use(convert(serve(path.resolve(__dirname, 'static'))))

// 发送文件,如HTML
app.use(async (ctx, next) => {
Expand Down
35 changes: 29 additions & 6 deletions template/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* @param {Object} from 被安装的对象
* @return {Object} to 安装完成的对象
*/
export function extend(to, from) {
let keys = Object.keys(from)
export function extend (to, from) {
const keys = Object.keys(from)
let i = keys.length
while (i--) {
to[keys[i]] = from[keys[i]]
Expand All @@ -18,10 +18,10 @@ export function extend(to, from) {
* @param {Number} len 要生成的长度
* @return {String} pwd 生成的随机字符串
*/
export function randomString(len) {  
export function randomString (len) {
len = len || 5
let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
let maxPos = chars.length
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
const maxPos = chars.length
let pwd = ''
for (let i = 0; i < len; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos))
Expand All @@ -34,7 +34,7 @@ export function randomString(len) {  
* @param {Object} extra 附加的说明与参数
*/
export class KoaErr extends Error {
constructor({ message = 'Error', status = 500 } = {}, ...args) {
constructor ({ message = 'Error', status = 500 } = {}, ...args) {
super()
this.message = message
this.status = status
Expand All @@ -43,3 +43,26 @@ export class KoaErr extends Error {
}
}
}

/**
* 上传文件
* @type {Object}
*/
import multer from 'koa-multer'
import fsp from 'fs-promise'

const storage = multer.diskStorage({
async destination (req, file, cb) {
const dir = `upload/${file.fieldname}/`
const exists = await fsp.exists(dir)
if (!exists) {
await fsp.mkdir(dir)
}
cb(null, dir)
},
filename (req, file, cb) {
cb(null, `${file.fieldname}-${file.originalname}`)
}
})

export const upload = multer({ storage: storage })
4 changes: 3 additions & 1 deletion template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@
"babel-register": "^6.8.0",
"babel-runtime": "^6.9.2",
"ejs": "^2.4.1",
"fs-promise": "^0.5.0",
"koa": "^2.0.0",
"koa-bodyparser": "^3.1.0",
"koa-compress": "^2.0.0",
"koa-convert": "^1.2.0",
"koa-cors": "^0.0.16",
"koa-json": "^1.1.1",
"koa-logger": "^1.3.0",
"koa-multer": "^1.0.0",
"koa-router": "^7.0.1",
"koa-send": "^3.2.0",
"koa-static": "^2.0.0",
"koa-views": "^5.0.1",
"nodemon": "^1.9.1"
}
}
}
2 changes: 1 addition & 1 deletion template/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ index
// 发送静态文件
await ctx.send(ctx, 'index.html', { root: 'static/index' })
})

export default index
4 changes: 4 additions & 0 deletions template/router/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Router from 'koa-router'
import { upload } from './../helper'

const test = new Router({
prefix: '/test'
Expand All @@ -16,5 +17,8 @@ test
.post('/post', (ctx, next) => {
ctx.body = ctx.request.body
})
.post('/file', upload.single('avatar'), async (ctx, next) => {
ctx.body = ctx.req.file
})

export default test
4 changes: 2 additions & 2 deletions template/run.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require("babel-register")
require('./app.js')
require('babel-register')
require('./app.js')

0 comments on commit 6603fb5

Please sign in to comment.