Our great sponsors
-
formidable
The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
import formidable from 'formidable'; /* global defineEventHandler, getRequestHeaders, readBody */ /** * @see https://nuxt.com/docs/guide/concepts/server-engine * @see https://github.com/unjs/h3 */ export default defineEventHandler(async (event) => { let body; const headers = getRequestHeaders(event); if (headers['content-type']?.includes('multipart/form-data')) { body = await parseMultipartNodeRequest(event.node.req); } else { body = await readBody(event); } console.log(body); return { ok: true }; });
Instantiate a multipart/form-data parser using a library called formidable
import formidable from 'formidable'; /* global defineEventHandler, getRequestHeaders, readBody */ /** * @see https://nuxt.com/docs/guide/concepts/server-engine * @see https://github.com/unjs/h3 */ export default defineEventHandler(async (event) => { let body; const headers = getRequestHeaders(event); if (headers['content-type']?.includes('multipart/form-data')) { body = await parseMultipartNodeRequest(event.node.req); } else { body = await readBody(event); } console.log(body); return { ok: true }; });
The backend is powered by Nuxt.js‘ Event Handler API, which receives an incoming request as an “event” object, detects whether it’s a multipart/form-data request (always true for file uploads), and passes the underlying Node.js request object (or IncomingMessage) to this custom function called parseMultipartNodeRequest.
Related posts
- Avoiding lock-in for your image pipeline with Nuxt Image and Netlify Image CDN
- Create your nuxt website with Netlify ✨
- File Uploads for the Web (3): File Uploads in Node & Nuxt
- Announcing Node.js 22.0.0: What’s New and Why It Matters
- How to Make a VS Code Extension Using TypeScript: A Step-by-Step Guide