Skip to content

Instantly share code, notes, and snippets.

@Ntropish
Last active July 10, 2018 17:21
Show Gist options
  • Save Ntropish/1c26966fbd9e06be8c7e87cde78dc830 to your computer and use it in GitHub Desktop.
Save Ntropish/1c26966fbd9e06be8c7e87cde78dc830 to your computer and use it in GitHub Desktop.
const express = require('express')
const bodyParser = require('body-parser')
const uuidv4 = require('uuid/v4')
const app = express()
const images = {}
// This is middleware that lets you get the post body in your endpoint handlers below
app.use(bodyParser.json())
// POST to the app with a dataUri in the post body
app.post('/', ({body: {dataUri}}, res) => {
// Generate a token that looks like an image
const token = `${uuidv4()}.png`
// Store the item in memory
images[token] = dataUri
// Remove the item after 10 minutes
setTimeout(() => { delete images[token] }, 1000 * 60 * 10)
// Give back the token
res.end(token)
})
// GET from the app with any token
app.get('/:token', (req, res) => {
// Fetch the token, or undefined if it doesn't exist
res.end(images[req.params.token])
})
app.listen(3000, () => console.log('Image Store activated on port 3000'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment