Skip to content

Form parser

formParser is a middleware that parses multipart/form-data. Other content types are ignored.

Text input values can be retrieved with req.body, and uploaded files with req.files.

import { formParser, UploadedFile } from '@shelepuginivan/lunatic'
import { writeFile } from 'fs/promises'
app.use(formParser)
app.post('/', async (req, res) => {
const [uploadedFile] = (req.files as Record<'file', UploadedFile[]>).file
await writeFile('new.txt', uploadedFile.data)
await res.status(200).json({ message: 'received form data' })
})
app.listen(8000)

UploadedFile

UploadedFile is the interface where files are available for retrieval from a request. It has the following properties:

  • data: Buffer — content of uploaded file
  • filename: string — name of uploaded file
  • mimetype: string — MIME-type of uploaded file