Skip to content

Instantly share code, notes, and snippets.

@SergeiMeza
Created January 2, 2020 11:37
Show Gist options
  • Select an option

  • Save SergeiMeza/c58856c423868a3098a61ad5692d71ee to your computer and use it in GitHub Desktop.

Select an option

Save SergeiMeza/c58856c423868a3098a61ad5692d71ee to your computer and use it in GitHub Desktop.
express receive file stream post request
import fs from "fs"
import express from "express"
app.post("/test-upload", async (req, res) => {
const mimetype = req.headers["content-type"]
const filename = req.headers["filename"]
prettyPrint({ mimetype, filename })
if (!mimetype || typeof mimetype !== "string") return res.status(403).send()
if (!filename || typeof filename !== "string") return res.status(403).send()
req.pipe(fs.createWriteStream(__dirname + "/uploads/" + filename))
req.on("error", () => {
return res.send({ success: false })
})
req.on("end", () => {
return res.send({ success: true })
})
})
app.listen({ port: 4000 })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment