Skip to content

Instantly share code, notes, and snippets.

@rmela
Created November 1, 2019 02:48
Show Gist options
  • Save rmela/ae7a9c4d1ff485c5248225a7e1262f91 to your computer and use it in GitHub Desktop.
Save rmela/ae7a9c4d1ff485c5248225a7e1262f91 to your computer and use it in GitHub Desktop.
Read raw upload data as stream in express js
const express = require('express')
const app = express()
const HOST = 'localhost'
const PORT = 8000
function hello( req, res ) {
const msg = `Use curl http://${HOST}:${PORT}/upload --upload-file <somefile>`
res.send(msg)
res.end()
}
function upload( req, res ) {
let bytes = 0
req.on('data', data => bytes += data.length )
req.on('end', () => res.end( `You uploaded ${bytes} bytes of data\n` ) )
}
app.get('/', hello )
app.put('/upload', upload )
let server = app.listen( PORT, HOST )
// Handle Expect: 100-Continue
server.on('checkContinue', function(req,res) {
res.writeContinue()
server.emit('request', req, res )
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment