Last active
December 11, 2015 05:48
-
-
Save PaulWoodIII/4554456 to your computer and use it in GitHub Desktop.
trying to a a /file-upload path to the node-cella example project to put images directly onto the mongodb, added modules "connect-multipart-gridform" "gridform" and "gridfs-stream"
This file contains hidden or 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
| var express = require('express'), | |
| path = require('path'), | |
| http = require('http'), | |
| wine = require('./routes/wines'), | |
| cup = require('./routes/cups'); | |
| var Grid = require('gridfs-stream'); | |
| var mongo = require('mongodb'); | |
| var Server = mongo.Server, | |
| Db = mongo.Db; | |
| var server = new Server('localhost', 27017, {auto_reconnect: true}); | |
| var db = new Db('winedb', server, {safe: true}); | |
| db.open(function(err, db) { | |
| if(!err) { | |
| console.log("Connected to 'winedb' database"); | |
| db.collection('fs', {safe:true}, function(err, collection) { | |
| if (!err) { | |
| console.log("The 'fs' collection doesn't exist. Creating it with sample data..."); | |
| var multipartoptions = { db: db, mongo: mongo }; | |
| app.use(multipart(multipartoptions)); | |
| } | |
| }); | |
| } | |
| }); | |
| var multipart = require('connect-multipart-gridform'); | |
| var formidable = require('formidable'); | |
| var app = express(); | |
| app.configure(function () { | |
| app.set('port', process.env.PORT || 3000); | |
| app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */ | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| //app.use(multipart(multipartoptions)); | |
| }); | |
| app.get('/wines', wine.findAll); | |
| app.get('/wines/:id', wine.findById); | |
| app.post('/wines', wine.addWine); | |
| app.put('/wines/:id', wine.updateWine); | |
| app.delete('/wines/:id', wine.deleteWine); | |
| app.get('/cups', cup.findAll); | |
| app.get('/cups/:id', cup.findById); | |
| app.post('/cups', cup.addCup); | |
| app.put('/cups/:id', cup.updateCup); | |
| app.delete('/cups/:id', cup.deleteCup); | |
| app.get('/lastfile'), function(req,res){ | |
| var readstream = gfs.createReadStream('crud.png'); | |
| readstream.pipe(res); | |
| //res.send(files); | |
| } | |
| app.post('/file-upload', function(req, res){ | |
| var form = multipart.gridform({db:db,mongo:mongo}); | |
| form.parse(req, function (err, fields, files) { | |
| if (err) { | |
| console.log('Error Uploading: ' + err); | |
| res.send({'Error':err}); | |
| } | |
| else { | |
| var thumb = files.thumbnail; | |
| console.log(thumb); | |
| console.log(form); | |
| console.log(form.gridfsStream); | |
| console.log(Grid); | |
| //gfs.createReadStream(thumb.name); | |
| //readstream.pipe(res); | |
| res.send(thumb); | |
| } | |
| }); | |
| }); | |
| http.createServer(app).listen(app.get('port'), function () { | |
| console.log("Express server listening on port " + app.get('port')); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment