Last active
October 31, 2016 04:58
-
-
Save jakehasler/4c86c451531de32659e3d8c56fa990ee to your computer and use it in GitHub Desktop.
Route for accepting the POST Image Upload Request
This file contains 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 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