我正在使用Multer学习koa2将图像发布到服务器。
像文件那样使用我的用法 它显示TypeError: fn.apply is not a function
at Object.<anonymous> (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.2.4.2@koa-route/index.js:34:19)
at next (native)
at onFulfilled (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.4.6.0@co/index.js:65:19)
at /Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.4.6.0@co/index.js:54:5
at Object.co (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.4.6.0@co/index.js:50:10)
at converted (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.1.2.0@koa-convert/index.js:17:15)
at dispatch (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.3.2.1@koa-compose/index.js:44:32)
at next (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.3.2.1@koa-compose/index.js:45:18)
at dispatch (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.7.0.0@koa-router/lib/router.js:315:32)
at dispatch (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.3.2.1@koa-compose/index.js:44:32)
at next (/Users/MacBook/Documents/nodeSutdy/nodeweb6/node_modules/.3.2.1@koa-compose/index.js:45:18)
at /Users/MacBook/Documents/nodeSutdy/nodeweb6/templating.js:32:15
at next (native)
at step (/Users/MacBook/Documents/nodeSutdy/nodeweb6/templating.js:1:191)
at /Users/MacBook/Documents/nodeSutdy/nodeweb6/templating.js:1:437
at /Users/MacBook/Documents/nodeSutdy/nodeweb6/templating.js:1:99
(node:2696) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: Cannot read property 'headers' of undefined
我的代码
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const controller = require('./controller');
const templating = require('./templating');
const route = require('koa-route');
const multer = require('koa-multer');
const session = require('koa-session');
const convert = require('koa-convert');
const mount_uploadify = require('koa-uploadify');
const upload = multer({dis:"./uploads"});
const isProduction = process.env.NODE_ENV === 'production';
const app = new Koa();
app.keys=['a','b'];
app.use(session(app));
// parse request body:
app.use(bodyParser());
// log request URL:
app.use(async (ctx,next) => {
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
var
start = new Date().getTime(),
execTime;
await next();
execTime = new Date().getTime() - start;
ctx.response.set('X-Response-Time', `${execTime}ms`);
});
// static file support:
if (! isProduction) {
let staticFiles = require('./static-files');
app.use(staticFiles('/static/', __dirname + '/static'));
}
// add nunjucks as view:
app.use(templating('views', {
noCache: !isProduction,
watch: !isProduction
}));
// add controller:
app.use(controller());
app.use(convert(route.post('/profile', upload.single('avatar'))));
app.listen(3000);