{"id":22030956,"url":"https://github.com/ts-stack/multer","last_synced_at":"2025-10-13T15:13:16.334Z","repository":{"id":250066902,"uuid":"833375524","full_name":"ts-stack/multer","owner":"ts-stack","description":"Is a node.js library for handling multipart/form-data, which is primarily used for uploading files.","archived":false,"fork":false,"pushed_at":"2024-09-25T10:37:52.000Z","size":2956,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T10:01:34.516Z","etag":null,"topics":["body-parser","multipart","multipart-formdata"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ts-stack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-24T23:29:10.000Z","updated_at":"2025-01-21T16:12:47.000Z","dependencies_parsed_at":"2024-07-25T01:54:59.681Z","dependency_job_id":null,"html_url":"https://github.com/ts-stack/multer","commit_stats":null,"previous_names":["ts-stack/multer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmulter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmulter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmulter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ts-stack%2Fmulter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ts-stack","download_url":"https://codeload.github.com/ts-stack/multer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252874292,"owners_count":21817801,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["body-parser","multipart","multipart-formdata"],"created_at":"2024-11-30T08:12:38.145Z","updated_at":"2025-10-13T15:13:16.274Z","avatar_url":"https://github.com/ts-stack.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @ts-stack/multer\n\nIs a node.js library for handling `multipart/form-data`, which is primarily used for uploading files. It is fork of [ExpressJS multer v2.0.0-rc.4][0].\n\n**NOTE**: Multer will not process any form which is not multipart (`multipart/form-data`).\n\n## Installation\n\n```sh\nnpm install @ts-stack/multer\n```\n\n## Usage\n\nMulter returns an object in Promise with four properties: `textFields`, `file`, `files` and `groups`. The `textFields` object contains the values of the text fields of the form, the `file`, `files` or `groups` object contains the files (as `Readable` stream) uploaded via the form.\n\nThe following example uses ExpressJS only for simplicity. In fact, `@ts-stack/multer` does not return middleware, so it is less convenient for ExpressJS than the [original module][0]. Basic usage example:\n\n```ts\nimport { Multer } from '@ts-stack/multer';\nimport express from 'express';\nimport { createWriteStream } from 'node:fs';\n\n// Here `avatar`, `photos` and `gallery` - is the names of the field in the HTML form.\nconst multer = new Multer({ limits: { fileSize: '10MB' } });\nconst parseAvatar = multer.single('avatar');\nconst parsePhotos = multer.array('photos', 12);\nconst parseGroups = multer.groups([\n  { name: 'avatar', maxCount: 1 },\n  { name: 'gallery', maxCount: 8 },\n]);\nconst app = express();\n\napp.post('/profile', async (req, res, next) =\u003e {\n  const parsedForm = await parseAvatar(req, req.headers);\n  // parsedForm.file is the `avatar` file\n  // parsedForm.textFields will hold the text fields, if there were any\n  const path = `uploaded-files/${parsedForm.file.originalName}`;\n  const writableStream = createWriteStream(path);\n  parsedForm.file.stream.pipe(writableStream);\n  // ...\n});\n\napp.post('/photos/upload', async (req, res, next) =\u003e {\n  const parsedForm = await parsePhotos(req, req.headers);\n  // parsedForm.files is array of `photos` files\n  // parsedForm.textFields will contain the text fields, if there were any\n  const promises: Promise\u003cvoid\u003e[] = [];\n  parsedForm.files.forEach((file) =\u003e {\n    const promise = new Promise\u003cvoid\u003e((resolve, reject) =\u003e {\n      const path = `uploaded-files/${file.originalName}`;\n      const writableStream = createWriteStream(path);\n      writableStream.on('error', reject);\n      writableStream.on('finish', resolve);\n      file.stream.pipe(writableStream);\n    });\n    promises.push(promise);\n  });\n\n  await Promise.all(promises);\n  // ...\n});\n\napp.post('/cool-profile', async (req, res, next) =\u003e {\n  const parsedForm = await parseGroups(req, req.headers);\n  // parsedForm.groups is an object (String -\u003e Array) where fieldname is the key, and the value is array of files\n  //\n  // e.g.\n  //  parsedForm.groups['avatar'][0] -\u003e File\n  //  parsedForm.groups['gallery'] -\u003e Array\n  //\n  // parsedForm.textFields will contain the text fields, if there were any\n});\n```\n\nIn case you need to handle a text-only multipart form, you can use the `.textFields()` method, example:\n\n```ts\nimport { Multer } from '@ts-stack/multer';\nimport express from 'express';\n\nconst parseFormFields = new Multer().textFields();\nconst app = express();\n\napp.post('/profile', async (req, res, next) =\u003e {\n  const parsedForm = await parseFormFields(req, req.headers);\n  // parsedForm.textFields contains the text fields\n});\n```\n\n## Error handling\n\nThis is a list of error codes:\n\n```ts\nconst errorMessages = new Map\u003cErrorMessageCode, string\u003e([\n  ['CLIENT_ABORTED', 'Client aborted'],\n  ['LIMIT_FILE_SIZE', 'File too large'],\n  ['LIMIT_FILE_COUNT', 'Too many files'],\n  ['LIMIT_FIELD_KEY', 'Field name too long'],\n  ['LIMIT_FIELD_VALUE', 'Field value too long'],\n  ['LIMIT_FIELD_COUNT', 'Too many fields'],\n  ['LIMIT_UNEXPECTED_FILE', 'Unexpected file field'],\n]);\n```\n\nYou can see these error codes in the `MulterError#code` property:\n\n```ts\nimport { Multer, MulterError, ErrorMessageCode } from '@ts-stack/multer';\n\n// ...\ntry {\n  const parse = new Multer().single('avatar');\n  const parsedForm = await parse(req, req.headers);\n  // ...\n} catch (err) {\n  if (err instanceof MulterError) {\n    err.code; // This property is of type ErrorMessageCode.\n    // ...\n  }\n}\n```\n\n[0]: https://github.com/expressjs/multer/tree/v2.0.0-rc.4\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fmulter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fts-stack%2Fmulter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fts-stack%2Fmulter/lists"}