Code Monkey home page Code Monkey logo

multer's People

Contributors

fundon avatar i5ting avatar linusu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

multer's Issues

2.0.0 release

If I am granted npm/GitHub access, here are my plans for 2.0.0 release:

  • expose ctx.file, ctx.files, ctx.request.file, and ctx.request.files versus binding to the Node request object (#23)
  • add multer as a peer dependency so streams can be used (#23

Trying out the example gives an error

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);

Error in cmd:
screen shot 2016-03-06 at 1 40 59 am

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!!!

Koa multer can not work with koa-router

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.

AssertionError [ERR_ASSERTION]: headers have already been sent

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.

Multer doesn't save an extensionand name of file

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.

TypeError: fn.apply is not a function

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

How to catch error throw in the middleware?

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 ?
})

Why parsed body not available on Koa's supercharged request?

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.

Multer crashes node (Cannot read property 'headers' of undefined)

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?

Access to stream underneath

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.

How to use with individual routes?

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
  ...
})

Middleware resolves before ctx.requests is populated with uploaded file information

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.