Created
November 1, 2019 02:48
-
-
Save rmela/ae7a9c4d1ff485c5248225a7e1262f91 to your computer and use it in GitHub Desktop.
Read raw upload data as stream in express js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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