Hi guys,I'm having a strange issue when I deploy my Express app to Heroku. Locally, all my GET, POST, and DELETE requests work fine, but when I push my project to Heroku, only the POST request fails with an error code of 500. All the other requests are still working fine. Has anyone here ever encountered a similar issue? I don't think it's a problem with my database because clearly my app can read from my MongoLab database as well as delete items in it. Perhaps it could be caused by something Heroku-specific? Here are my relevant files if you want to take a look:bin/www (The executable file for my Express app):#!/usr/bin/env node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('backend:server'); var http = require('http'); /** * Get port from environment and store in Express. */ var port = normalizePort(process.env.PORT || '3001'); app.set('port', port); /** * Create HTTP server. */ var server = http.createServer(app); /** * Listen on provided port, on all network interfaces. */ server.listen(port); server.on('error', onError); server.on('listening', onListening); /** * Normalize a port into a number, string, or false. */ function normalizePort(val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { // port number return port; } return false; } /** * Event listener for HTTP server "error" event. */ function onError(error) { if (error.syscall !== 'listen') { throw error; } var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error(bind + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error(bind + ' is already in use'); process.exit(1); break; default: throw error; } } /** * Event listener for HTTP server "listening" event. */ function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); } App.js: var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var multer = require('multer'); var index = require('./routes/index'); var users = require('./routes/users'); var photos = require('./routes/photos'); var places = require('./routes/places'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './tmp') }, filename: function (req, file, cb) { cb(null, Date.now() + '-' + file.originalname) //Appending .jpg } }) app.use(multer({ storage: storage }).array('file')); app.use('/', index); app.use('/users', users); app.use('/api/photos', photos); app.use('/api/places', places); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app; routes/Photos.js:var express = require('express'); var router = express.Router(); var mongoose = require('mongoose'); var fs = require('fs'); var path = require('path'); var uid = require('uid-safe'); var mime = require('mime'); var util = require('util'); var MONGOLAB_URI = process.env.MONGOLAB_URI ? process.env.MONGOLAB_URI : 'mongodb://leea8:albertdb@ds121464.mlab.com:21464/albert-site-db'; mongoose.connect(MONGOLAB_URI); var db = mongoose.connection; var Image = require('./schemas/ImageSchema'); var Marker = require('./schemas/MarkerSchema'); var IMAGE_TYPES = ['image/jpeg', 'image/png']; var cloudinary = require('cloudinary'); cloudinary.config({ cloud_name: 'albert-site-photos', api_key: '748939627676922', api_secret: 'sD2c8Vi18_jb4Gz03pNCtDb87rI' }); /* GET users listing. */ router.get('/', function(req, res, next) { console.log('Photos Get') Image.find() .exec(function(err, images) { if (!images) { res.send("Error: No Photos"); } else { res.json(images); } }); }); router.post('/uploads', function(req, res, next) { console.log('Photos Post'); if (!req.files) { return res.send(415, 'No file submitted'); } var type = req.files[0].mimetype; if (IMAGE_TYPES.indexOf(type) == -1) { return res.send(415, 'Unsupported image type: Must be jpeg, jpg, jpe, or png'); } var tempPath = req.files[0].path; cloudinary.v2.uploader.upload(tempPath, {angle: "exif"}, function (err, result) { if (err) { return handleError(err); } var tagArray = getTagArray(req.body.tags); var newImage = new Image({ src: result.url, thumbnail: result.url, thumbnailWidth: result.width, thumbnailHeight: result.height, caption: req.body.caption, location: req.body.location, tags: tagArray, date: req.body.date, coordinates: { lat: req.body.lat, lng: req.body.lng }, }); console.info(req.body.tags); newImage.save(function(err) { if (err) return handleError(err); }); }); deleteTempFile(tempPath); addToMarkers(req); console.log(res); res.send('Post Finished'); }); router.delete('/delete', function(req, res, next) { console.log('delete request'); cloudinary.v2.api.delete_all_resources(); Image.remove(function(err) { if (err) return handleError(err); }); Marker.remove(function(err) { if (err) return handleError(err); }); res.send('Delete Finished'); }) //Helper functions/methods function deleteTempFile(tempPath) { fs.unlink(tempPath, function(err) { if (err) return handleError(err); }) } function addToMarkers(req) { var lat = req.body.lat; var lng = req.body.lng; var key = req.body.location var newMarker = new Marker({ position: { lat: lat, lng: lng }, key: key, defaultAnimation: 2, }); Marker.findOne({ 'position.lat': lat, 'position.lng': lng }) .exec(function(err, marker) { if (!marker) { newMarker.save(function(err) { if(err) return handleError(err); }); } }) } function getTagArray(tags) { var tagValues = tags.split(' '); var tagArray = []; var length = tagValues.length; var tag; var tag_sc; // Tag in Start Case format (Only first letter capitalized) for (var i = 0; i < length; i++) { console.log(tagValues[i]); if (tagValues[i].replace(/\s/g, '').length > 0) { tag = tagValues[i].toLowerCase(); tag_sc = tag.charAt(0).toUpperCase() + tag.slice(1); tagArray.push({ title: tag_sc, value: tag_sc }); } } return tagArray; } module.exports = router; package.json:{ "name": "my-server", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "engines": { "node": "8.6.0" }, "dependencies": { "body-parser": "^1.17.2", "cloudinary": "^1.9.0", "cookie-parser": "~1.4.3", "cors": "^2.8.4", "crypto": "^1.0.1", "debug": "~2.6.3", "express": "~4.15.2", "jade": "~1.11.0", "mime": "latest", "mkdirp": "^0.5.1", "mongoose": "^4.11.8", "morgan": "~1.8.1", "multer": "^1.3.0", "serve-favicon": "~2.4.2", "uid-safe": "^2.1.5", "uid2": "0.0.3", "util": "^0.10.3" } }
Submitted October 02, 2017 at 10:40PM by chzits
No comments:
Post a Comment