Last active
November 26, 2020 17:16
-
-
Save amalj07/d41b7d0229229b127e32abfed8b22c40 to your computer and use it in GitHub Desktop.
Node.js API to upload file to firebase cloud storage
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 multer = require('multer') | |
const firebase = require('./firebase') | |
const app = express() | |
const upload = multer({ | |
storage: multer.memoryStorage() | |
}) | |
app.post('/upload', upload.single('file'), (req, res) => { | |
if(!req.file) { | |
res.status(400).send("Error: No files found") | |
} else { | |
const blob = firebase.bucket.file(req.file.originalname) | |
const blobWriter = blob.createWriteStream({ | |
metadata: { | |
contentType: req.file.mimetype | |
} | |
}) | |
blobWriter.on('error', (err) => { | |
console.log(err) | |
}) | |
blobWriter.on('finish', () => { | |
res.status(200).send("File uploaded.") | |
}) | |
blobWriter.end(req.file.buffer) | |
} | |
}) | |
app.listen(5000, () => { | |
console.log('🚀Server listening on port 5000') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment