Improve developer experience
See original GitHub issue🚀 Feature Proposal
Today, I needed to develop a simple image upload service in fastify and it felt unsatisfying.
Motivation
Handling multipart form data should be dead simple and intuitive in fastify.
- Support
async / await
(async stream iterators) - Zero boilerplate, provide fastify-hooks to do all this stuff with good defaults (like in expressjs/multer
upload.single("uploaded_file")
) - Delegate busboy failures to fastify (like in expressjs https://github.com/expressjs/multer/blob/6b5fff5feaf740f249b1b2858e5d06009cbd245c/lib/make-middleware.js#L167-L170)
Example
In https://eggjs.org/en/basics/controller.html#acquiring-the-submitted-files you can easily switch different modes without dealing with too much boilerplate.
// Single file in disk storage mode
app.post("/images", async (req, res) => {
req.file.filename
req.file.mimetype
// async / await
})
// Multiple files in disk storage mode
app.post("/images", async (req, res) => {
req.files
})
// Single file in stream mode
app.post("/images", async (req, res) => {
const stream = await ctx.getFileStream();
})
// Multiple files in stream mode
app.post("/images", async (req, res) => {
const parts = ctx.multipart();
let part;
// parts() return a promise
while ((part = await parts()) != null) {}
})
in fastify you need for the same behaviour a lot of boilerplate and you are responsible to handle all cases manually.
fastify.register(require('fastify-multipart'))
fastify.post('/upload', function (req, reply) {
const mp = req.multipart(handler, function (err) {
console.log('###2');
if (err) {
reply.send(err)
return
}
console.log('upload completed', process.memoryUsage().rss)
reply.code(200).send()
})
mp.on('field', function (key, value) {
console.log('form-data', key, value)
})
function handler (field, file, filename, encoding, mimetype) {
pump(file, fs.createWriteStream('a-destination'), (err) => {
console.log('###1');
})
}
})
Logs:
server listening on 3000
###2
upload completed 31477760
###1
Additional problems for the users:
onEnd
is fired before the file was written.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Five proven approaches for a better Developer Experience in ...
Five proven approaches for a better Developer Experience in your organisation · 1. Adopt product thinking for technical products and platforms · 2 ......
Read more >How to Build a Great Developer Experience: A Practical Guide
1. Optimize all processes. Every process matters when building a great developer experience. · 2. Automate developers' workflow · 3. Provide customizable tools ......
Read more >Top 11 Developer Experience Tools to Improve DX | Clockwise
11 Developer experience tools and strategies to improve DX · Start with company culture · Developer onboarding · Understand the developer journey ·...
Read more >Good Developer Experience
The Developer Experience (DX) describes the experience developers have while using or working on your product. It is a package of positive and...
Read more >How can I improve my developer experience? - Launchable
Five approaches that answer “How can I improve my developer experience?” · 1. Know thy enemy: Identify existing and potential risks to your ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Since this is the core package we should try to improve this package or switch to fastify-multer entirely.
We might also move fastify-multer under fastify organization here on GitHub.