Authored by 陈轩

add uploadImage api

/**
* 上传接口
* @author: kai.bi<kai.bi@yoho.cn>
* @date: 2016/07/22
*/
'use strict';
const request = require('request-promise');
const fs = require('fs');
const _ = require('lodash');
const uploadImg = (req, res, next) => {
let files = req.files && req.files.filename || [];
if (!_.isArray(files)) {
files = [files];
}
req.body.files = [];
files.forEach(file => {
req.body.files.push(fs.createReadStream(file.path));
req.body.files.push(file.name);
});
request({
method: 'post',
url: 'http://upload.static.yohobuy.com',
formData: {
fileData: req.body.files,
project: req.body.bucket || 'goodsimg'
},
json: true
}).then(function(result) {
res.json(result);
}).catch(next);
};
module.exports = {
uploadImg
};
... ...
/**
* sub app api
* @author: 陈轩<xuan.chen@yoho.cn>
* @date: 2016/09/02
*/
var express = require('express'),
path = require('path'),
hbs = require('express-handlebars');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
});
app.set('views', path.join(__dirname, 'views/action'));
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],
helpers: global.yoho.helpers
}));
// router
app.use(require('./router'));
module.exports = app;
... ...
/**
* router of sub app api
* @author: 陈轩<xuan.chen@yoho.cn>
* @date: 2016/09/02
*/
'use strict';
const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
const uploadApi = require(cRoot + '/upload.js');
router.post('/upload/image', multipartMiddleware, uploadApi.uploadImg);
module.exports = router;
... ...
... ... @@ -13,6 +13,7 @@ module.exports = app => {
app.use(require('./apps/passport'));
// 业务模块
app.use('/api', require('./apps/api')); // 各模块公有 API
app.use('/product', require('./apps/product'));
app.use('/guang', require('./apps/guang'));
app.use('/activity', require('./apps/activity'));
... ...