Skip to content

Instantly share code, notes, and snippets.

@madeinfree
Created October 25, 2017 05:45
Show Gist options
  • Save madeinfree/a8ff571e1e3110ef80daacf666df9b3c to your computer and use it in GitHub Desktop.
Save madeinfree/a8ff571e1e3110ef80daacf666df9b3c to your computer and use it in GitHub Desktop.
const express = require('express');
const app = express();
app.post('/image-upload', (req, res) => {
let body = '';
let len = parseInt(req.get('content-length'), 10);
req.on('data', chunk => {
body += chunk;
});
req.on('end', () => {
const chunkLen = parseInt(body.length, 10);
const json = JSON.parse(body);
var base64Data = json.url.replace(/^data:image\/png;base64,/, '');
if (chunkLen === len) {
require('fs').writeFile('out.png', base64Data, 'base64', function(err) {
console.log(err);
});
return res.status(200).send('Upload file success.');
}
return res.status(200).send('Upload file failed.');
});
});
app.listen(3001, () => {
console.log('Server listen on port 3001');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment