koa-modules / multer Goto Github PK
View Code? Open in Web Editor NEWMiddleware for handling `multipart/form-data` for koa, based on Express's multer.
License: MIT License
Middleware for handling `multipart/form-data` for koa, based on Express's multer.
License: MIT License
I am using Koa v1.1.2
My code is:
import koa from 'koa';
import bodyParser from 'koa-bodyparser';
import multer from 'koa-multer';
import {port} from './global_config';
import cors from 'kcors';
import {securedRoute, publicRoute} from './app/route';
const app = koa();
app.use(cors());
app.use(bodyParser());
app.use(publicRoute.routes());
app.use(publicRoute.allowedMethods());
app.use(securedRoute.routes());
app.use(securedRoute.allowedMethods());
app.use(multer({ dest: './uploads/'}));
// Start server
app.listen(port || 3000);
What should I do?
My second question is: what will be the koa-multer version of this original multer's code:
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})
I mean, I need unique multer configs for each uploading routes in the project.
Please help!!!
Hi, this multer can not work with alexmingoia/koa-router (My koa is 1.x) My code is:
const uploader = require('koa-multer')({dest: path.join(__dirname, '../upload')}); const router = new (loadLib('koa-router'))(); router.post('/lo/banner/img', uploader.single('image'), function *() {...})
When upload a file, it will throw an Error below:
TypeError: next[(intermediate value)] is not a function
at Object.allowedMethods (/home/zsl/git/iooly/server/LockscreenManager/node_modules/koa-router/lib/router.js:374:12)
at next (native)
at Object.allowedMethods (/home/zsl/git/iooly/server/LockscreenManager/node_modules/koa-router/lib/router.js:374:12)
at next (native)
at Object.dispatch (/home/zsl/git/iooly/server/LockscreenManager/node_modules/koa-router/lib/router.js:336:14)
at next (native)
at Object.serve (/home/zsl/git/iooly/server/LockscreenManager/node_modules/koa-static/index.js:43:14)
at next (native)
at Object.serve (/home/zsl/git/iooly/server/LockscreenManager/node_modules/koa-static/index.js:43:14)
at next (native)
at onFulfilled (/home/zsl/git/iooly/server/LockscreenManager/node_modules/co/index.js:65:19)
at /home/zsl/git/iooly/server/LockscreenManager/node_modules/co/index.js:54:5
at Object.co (/home/zsl/git/iooly/server/LockscreenManager/node_modules/co/index.js:50:10)
at Object.toPromise (/home/zsl/git/iooly/server/LockscreenManager/node_modules/co/index.js:118:63)
at next (/home/zsl/git/iooly/server/LockscreenManager/node_modules/co/index.js:99:29)
at onFulfilled (/home/zsl/git/iooly/server/LockscreenManager/node_modules/co/index.js:69:7)
So I make some changes with koa-multer and make it work with koa-router successful.The code I changed is: iooly/iooly-koa-multer.
This is my code.
import Koa from 'koa';
import Router from 'koa-router';
import multer from 'koa-multer';
const router = new Router();
const app = new Koa();
const uploadMulter = multer({ dest: 'public/upload/' });
router.post('/upload', uploadMulter.array('files'), async (ctx, next) => {
ctx.status = 200;
ctx.body = { answer: 'ok' };
return next();
});
When I upload files, it upload to the dir successfully, but when I set ctx.status
or ctx.body
, the log prints this, and return to client 404 (Not Found)
(node:7299) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError [ERR_ASSERTION]: headers have already been sent
(node:7299) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
From client:
const xhr = new XMLHttpRequest;
xhr.open('POST', '/some/url', true);
xhr.send(new FormData(form));
On server:
import Router from 'koa-router';
import path from 'path';
import multer from 'koa-multer';
const upload = multer({dest: path.join(__dirname, 'pdf')});
const router = new Router();
router.post('some/url', upload.single('file'), controller)
When multer is saving, it gener for file random name without extension.
Some problems come to me when I try to use typescript to write my web app, so i wonder if you could write an index.d.ts file for this project to support vscode intellisense or make it easy for us to write code by typescript?
Got the same error with using both Koa-v1.x and Koa-v2.x. Also tried koa-router and koa-route, and usage example to no avail. This is the error I get:
TypeError: fn.apply is not a function
at Object. (E:\Sites\test\node_modules\koa-route\index.js:34:19)
at next (native)
at onFulfilled (E:\Sites\test\node_modules\co\index.js:65:19)
at E:\Sites\test\node_modules\co\index.js:54:5
at Object.co (E:\Sites\test\node_modules\co\index.js:50:10)
at converted (E:\Sites\test\node_modules\koa-convert\index.js:17:15)
at dispatch (E:\Sites\test\node_modules\koa\node_modules\koa-compose\index.js:44:32)
at E:\Sites\test\node_modules\koa\node_modules\koa-compose\index.js:36:12
at Server. (E:\Sites\test\node_modules\koa\lib\application.js:136:7)
at emitTwo (events.js:106:13)
(node:4212) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: Cannot read property 'headers' of undefined
Just set file size limit can server throws an error with 500 status code.
I need to catch Error:
File too large` and throw 416 code.
router.post('/upload', upload.single('file'), async ctx => {
// how to catch error here ?
})
This project has been inactive since 2017. Is the project dead?
I was wondering why the parsed body is not made available to Koa's this.request
.
Using the package I could only find body
attached to this.req
.
I know this follows closely express/multer, but this is somehow confusing when using with Koa. All Koa's middlewares tend to attach additional work to this.request
instead of this.req
(actually koa itself is built following that philosophy).
Let me know. Cheers.
Koa and multer cause node to crash because req.headers
is undefined.
The error happens here (node_modules/type-is/index.js:93:13):
function hasbody (req) {
return req.headers['transfer-encoding'] !== undefined ||
!isNaN(req.headers['content-length'])
}
My simple node code:
const router = require('koa-router')()
const multer = require('koa-multer')
const upload = multer({ dest: 'uploads/' })
router.post('/files', upload.any(), function (next) {
console.log(this.request)
this.body = 'done'
})
When a the multipart request arrives at the endpoint, this error is thrown:
API Error { TypeError: Cannot read property 'headers' of undefined
at hasbody (/Users/John/test/node_modules/type-is/index.js:93:13)
at typeofrequest (/Users/John/test/node_modules/type-is/index.js:127:8)
at multerMiddleware (/Users/John/test/node_modules/multer/lib/make-middleware.js:18:10)
at Promise (/Users/John/test/node_modules/koa-multer/index.js:32:33)
at Object.<anonymous> (/Users/John/test/node_modules/koa-multer/index.js:31:14)
at Object.dispatch (/Users/John/test/node_modules/koa-router/lib/router.js:329:52)
at next (native)
at onFulfilled (/Users/John/test/node_modules/co/index.js:65:19)
at /Users/John/test/node_modules/co/index.js:54:5
at Object.co (/Users/John/test/node_modules/co/index.js:50:10)
at Object.toPromise (/Users/John/test/node_modules/co/index.js:118:63)
at next (/Users/John/test/node_modules/co/index.js:99:29)
at onFulfilled (/Users/John/test/node_modules/co/index.js:69:7)
at /Users/John/test/node_modules/co/index.js:54:5
at Object.co (/Users/John/test/node_modules/co/index.js:50:10)
at Object.toPromise (/Users/John/test/node_modules/co/index.js:118:63)
Does this have to do with Koa changing req
to this.request
?
You should improve this tool's documentation guys!
Hi, I may have missed something. Is there any way to access file data with streams? I can see two options only: download to the disk straightforward or store in memory as a buffer.
I think multer should write to ctx.request
because that is the koa request object currently it is writing to the ctx.req
which is the original node request object.
In my opinion this change would align the behaviour of this module with the koa ecosystem.
Not understanding how this can be done with koa-multer...
const multer = require('koa-multer')
const multerMiddleware = multer({ dest: './.uploads/' })
router.post('/files', multerMiddleware, function (next) {
console.log(this.req.files) // undefined
console.log(this.request.files) // undefined
...
})
I am running into a strange issue. After switching to Google Cloud, all my upload API endpoints are failing. The file is being uploaded successfully to my temporary directory, but it seems that the middleware is resolving before ctx.requests
is populated with the information about the file. This might only occur on highly spec'ed servers.
From this koa issue and comment, it was recommended that instead of .then(next)
, you resolve(next())
. I didn't have time to try this out myself so I just switched to another upload-handling middleware, but this may be something that might be helpful.
New version of multer is available, could this package be upgraded?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.