Skip to content

Instantly share code, notes, and snippets.

@jakehasler
Last active October 31, 2016 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakehasler/4c86c451531de32659e3d8c56fa990ee to your computer and use it in GitHub Desktop.
Save jakehasler/4c86c451531de32659e3d8c56fa990ee to your computer and use it in GitHub Desktop.
Route for accepting the POST Image Upload Request
const express = require('express');
const router = express.Router();
const Multer = require('multer');
const imgUpload = require('../modules/imgUpload');
// Handles the multipart/form-data
// Adds a .file key to the request object
// the 'storage' key saves the image temporarily for in memory
// You can also pass a file path on your server and it will save the image there
const multer = Multer({
storage: Multer.MemoryStorage,
fileSize: 5 * 1024 * 1024
});
// the multer accessing the key 'image', as defined in the `FormData` object on the front end
// Passing the uploadToGcs function as middleware to handle the uploading of request.file
router.post('/image-upload', multer.single('image'), imgUpload.uploadToGcs, function(request, response, next) {
const data = request.body;
if (request.file && request.file.cloudStoragePublicUrl) {
data.imageUrl = request.file.cloudStoragePublicUrl;
}
response.send(data);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment