Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChris Gaona
Full Stack JavaScript Techdegree Graduate 31,350 PointsWhy does my javascript block work on local machine but not in prod using Heroku?
I've been trying to figure out this problem for the past few days and haven't had any luck. I've searched on Google, and contacted Heroku, but haven't found anything useful. Please help! There is a piece of javascript code in my node.js app that works perfect on my local machine but then does not work in production on Heroku. I'm not sure what will be needed to help me, but I can't figure it out.
Here is the code that works on my local machine but not in production:
router.post('/postblog', upload.single('image'), function(req, res) {
//rename file to be name from local computer
fs.renameSync(req.file.destination + req.file.filename, req.file.destination + req.file.originalname);
//Code for posting to json file...uses name="" in html form
var imageWords = req.file.originalname;
var titleWords = req.body.name;
console.log(req.body.name);
var postLink = req.body.name.replace(/\s+/g, '-');
console.log(req.body.name.replace(/\s+/g, '-'));
var author = req.body.author;
console.log(req.body.author);
var paragraph = req.body.blogpost;
console.log(req.body.blogpost);
var date = req.body.date;
console.log(req.body.date);
var keywords = req.body.keywords;
console.log(req.body.keywords);
var newPost = { //new post object
"title": titleWords,
"link": postLink, //replace spaces with - (dash)
"paragraph": paragraph,
"author": author,
"date": date, //transform to different date format
"picture": "/static/images/" + imageWords,
"keywords": ""
}
//location of JSON file
var postFile = './public/json/blogs.json';
//Read JSON file
var configPost = fs.readFileSync(postFile);
//parse JSON file
var parsePost = JSON.parse(configPost);
//push new item to JSON
parsePost['blogs'].push(newPost);
//stringify file back to JSON format
var stringifyPost = JSON.stringify(parsePost, null, 4);
//Write new JSON to file
fs.writeFileSync(postFile, stringifyPost)
console.log('Success!');
req.flash('message2', 'Success! Blog Post Added!');
res.redirect('/dashboard');
});
I'm using the Multer module in the above code. On the Heroku logs there are no errors when I try to use the above javascript code. One problem is that only some of the console.logs that I have show up in the log, which is kind of odd. I can't figure out what I'm doing wrong! Thank you in advance!