Created
October 2, 2016 10:34
-
-
Save JaniKibichi/6d7e793c9c25b68cc38b66e9680790ee to your computer and use it in GitHub Desktop.
Sample code about uploading pictures to cloudinary using multer - NodeJs ExpressJS Framework
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
'use strict'; | |
var express = require("express"); | |
var multer = require('multer'); | |
var app = express(); | |
var options = require('./config/config') | |
var mongoose = require('mongoose'); | |
/* photo manenos */ | |
var cloudinary = require('cloudinary'); | |
/* configure mongoose */ | |
mongoose.connect('mongodb://localhost:27017/theToDos'); | |
/* include models */ | |
var Photo= require('./models/photos'); | |
/* actual work */ | |
var upload = multer({ dest : '../public/uploads'}).single('userPhoto'); | |
app.use(express.static(__dirname + '/public')); | |
app.post('/api/photo', function(req,res){ | |
upload(req, res, function(err){ | |
if(err){ return res.end("Error")}; | |
console.log(req); | |
res.end("file uploaded") | |
cloudinary.config({ | |
cloud_name: options.cloudinary.cloud_name, | |
api_key: options.cloudinary.api_key, | |
api_secret: options.cloudinary.api_secret | |
}); | |
cloudinary.uploader.upload(req.file.path, function(result) { | |
console.log(result); | |
//create an urembo product | |
var photo = new Photo(); | |
photo.name = req.body.name; | |
photo.picture = result.url; | |
photo.place = req.body.place; | |
photo.city = req.body.city; | |
//save the product and check for errors | |
photo.save(function(err, photos){ | |
if(err) | |
res.send(err); | |
res.json({ message: 'photographed place created.'}); | |
console.log(photos); | |
}); | |
}); | |
}); | |
}); | |
app.get('/api/photos', function(req, res){ | |
Photo.find(function( err, photos){ | |
if(err) | |
res.send(err); | |
res.json(photos); | |
}); | |
}); | |
app.listen(3000, function(){ | |
console.log("Working on port 3000"); | |
}); |
You should write the code for multiple image upload, I can write it for you if you don't mind.
wanted to know if you've written this, would be of help to me. been looking for a sample cuz I keep getting error anytime I use upload.array
thanks
I'm not sure I did.
But I'll help check my old repositories.
I'm not sure I did.
But I'll help check my old repositories.
Alright
Thanks
can't we call the mongoose save function outside of Cloudinary upload callback, I mean what if we need to save to the database based on conditions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should write the code for multiple image upload, I can write it for you if you don't mind.