node heroku error: connect ECONNREFUSED - heroku

I have this CRUD app on heroku connected to Mlab. I have 2 different routes (that submits from a form) data to MLab and an image to an S3 bucket. One route works completely fine. The other route, which is very similar to the first, minus some text inputs, throws an error on Heroku whenever you submit. It actually works fine locally though. If I remove all of the logic associated with the image upload it works too. I'm not sure where I'm going wrong here. I can't figure out why it would work on one almost identical route, but not the other.
This is my app.js code that connects mongoose and router
var router = express.Router();
var eventRoutes = require('./routes/event');
var adminRoutes = require('./routes/admin');
var venueRoutes = require('./routes/venue');
app.use('/', router);
app.use(eventRoutes);
app.use(adminRoutes);
app.use(venueRoutes);
mongoose.connect(process.env.HOTNIGHTDATABASEURL);
app.listen(process.env.PORT || 3000, function(){
var date = new Date()
console.log('Server Spinning Up at ' + date.toLocaleTimeString('en-US',
{hour12: true,}));
});
This is the config for S3 and the post route for the route that works.
//Multers3 and AWS s3 setup
var s3 = new aws.S3({
accessKeyId: process.env.HOTNIGHTEVENTSBUCKETID,
secretAccessKey: process.env.HOTNIGHTEVENTSBUCKETSECRET,
region: 'us-east-2'
});
var upload = multer({
storage: multerS3({
s3: s3,
acl: 'public-read',
bucket: 'hot-night-events',
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now().toString())
}
})
});
router.post('/events', upload.single('image'), function(req, res){
var event = new Event({
... for brevity left out data collected from form
});
event.save(function(err, data){
if(err){
console.log(err);
} else {
res.redirect('/');
}
});
});
This is the other route (in a different file) that doesn't work:
//Multers3 and AWS s3 setup
var s3 = new aws.S3({
accessKeyId: process.env.HOTNIGHTEVENTSBUCKETID,
secretAccessKey: process.env.HOTNIGHTEVENTSBUCKETSECRET,
region: 'us-east-2'
});
var upload = multer({
storage: multerS3({
s3: s3,
acl: 'public-read',
bucket: 'hot-night-events',
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
cb(null, Date.now().toString())
}
})
});
router.post('/venues', upload.single('venueImage'), function(req, res){
var venue = new Venue({
... for brevity left out data collected from form
});
venue.save(function(err, data){
if(err){
console.log(err);
} else {
res.redirect('/');
}
});
});
Any help would be greatly appreciated.

Related

ffmpeg using NestJS on ubuntu

I'm trying to use ffmpeg to generate a thumbnail for a video in input.
I'm using NestJS and hosting it on an AWS EC2 instance (ubuntu).
This is the generation and upload method I'm using and it's working perfectly well when I run it on localhost, but on ubuntu server it doesn't even generate the thumbnail and I don't know why ..
getVideoThumbnail(sourcePath: string): string {
const sourceName = path.parse(sourcePath).name + '_thumb.png';
ffmpeg({ source: sourcePath })
.on('end', () => {
this.logger.log("Thumbnail generated and uploaded successfully");
const uploadFile = () => {
const filePath = './thumbnails/' + sourceName; // file to upload
const params = {
Bucket: process.env.BUCKET_NAME,
Key: sourceName,
Body: fs.createReadStream(filePath),
ContentType: 'image/png',
ACL: 'public-read',
};
this.s3.upload(params, function (s3Err, data) {
if (s3Err) throw s3Err
if (data) {
// Unlink file - remove it from local storage
}
});
};
uploadFile();
})
.on('error', function (err, stdout, stderr) {
this.logger.error("Thumbnail generation failed:\n", err);
})
.thumbnail({
filename: sourceName,
count: 1,
}, './thumbnails/');
return `https://${process.env.BUCKET_NAME}.s3.amazonaws.com/${sourceName}`;;
}
}
I installed ffmpeg package on ubuntu but still the generation keeps on failing, and even the error log is empty so I can't figure out what is missing.
Do you guys have an idea ?

Display image on pug

I'm new to NodeJS world.
I tried to display image from mongodb using pug and node express.
Please see my code and guide me.
customer.pug
extend layouts
block content
h1.container #{title} of #{customer.full_name}
div.container
p Full Name: #{customer.full_name}
p Address: #{customer.address}
p Age: #{customer.age}
img.imageScr1(src=customer.profileimage, alt=customer.full_name)
app.js
const express = require('express');
const path = require('path');
const router = express.Router();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const fs = require('fs');
const multer = require('multer');
// set storage engine
const storage = multer.diskStorage({
destination: function(req, file, cb){
cb(null, './uploads/',)
},
filename: function (req, file, cb) {
cb(null, file.fieldname + Date.now() + path.extname(file.originalname));
}
});
// init upload
const upload = multer({
storage: storage
});
// Middlewares
// middleware for Assets - to set public for assets
app.use(express.static(path.join(__dirname, 'public')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// route - homepage
app.get('/', function (req, res) {
Customer.find({}, null, function(err, customers) {
if(err){
console.log(err);
} else{
// customers.profileimage = '/' + customers.profileimage;
// console.log('image prefix::: ' + customer.profileimage);
res.render('index', {
title: 'Customer List',
customers: customers
});
}
});
});
app.post('/customer/add', upload.single('profileimage'), function (req, res) {
var customer = new Customer();
customer.full_name = req.body.full_name;
customer.address = req.body.address;
customer.age = req.body.age;
customer.profileimage = req.file.path;
customer.save(function (err) {
if(err){
console.log(err);
} else{
res.redirect('/');
}
});
});
I got following error when render customer.pug view.
error_console
I found that, if I add '\' before image path 'uploads\profileimage1520473825415.jpg', it displays image.
I tried many ways to do that did not work.
Please help me.
Cheers

Data are not inserted in mongodb, it gives could not get any response error in postman

I build the project using the mean stack. I insert record using postman but I get one error, I try to resolve much time but not to find where is a mistake.
Postman error
This is models user.js file.which is shows userSchema details.
Models -> user.js
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
var config = require('../config/database');
// User Schema
var UserSchema = mongoose.Schema({
name: {
type: String,
},
email: {
type: String,
required: true
},
username: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.getUserById = function(id, callback){
User.findById(id, callback);
}
module.exports.getUserByUsername = function(username, callback){
var query = {username: username}
User.findOne(query, callback);
}
module.exports.addUser = function(newUser, callback){
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) throw err;
newUSer.password = hash;
newUser.save(callback);
});
});
}
This is Routes user.js file. here router of register displays, also define add user status results that can be shown if postman value is inserted in mongo then successfully another wise unsuccessfull message.
Routes -> users.js
var express = require('express');
var router = express.Router();
var password = require('passport');
var jwt = require('jsonwebtoken');
var User = require('../models/user');
// Register
router.post('/register', function(req, res, next){
let newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
});
User.addUser(newUser, (err, user) => {
if(err){
res.json({success: false, msg:'Failed to Register User'});
} else {
res.json({success: true, msg:'User Registered'});
}
});
});
// Authenticate
router.post('/authenticate', function(req, res, next){
res.send('Authenticate');
});
// Profile
router.get('/profile', function(req, res, next){
res.send('Profile');
});
module.exports = router;
You are sending the request wrongly from Postman. You should not use x-www-form-urlencoded. Use the Raw option at send it as a json object, like this:
{
"name": "Mark Melton",
"email": "xyz#gmail.com",
"username": "shuta",
"password": "Sunala123"
}
This is how it will work, because in your routes, you are using req.body...

Is my express post request is correct?

I'm trying to understand how Post Request works with express. I my case I would like to be able to update an web API call (is this the right term ?) with Google Analytics API. Most of the examples I found here or there are about handling post message or sign in operation.
Following this question here is what I have understand. On the front-end, using axios here is what I need to pass :
axios.post("http://localhost:3000/endpoints", my_parameters)
I can easily send this on the server side with react/redux. however on the server side, I'm kind of lost. I've got the following API call :
var key = require('./client_id.json')
var jwtClient = ...
var VIEW_ID = "ga:80820965";
var authorize = function( cb ) {
jwtClient.authorize(function(err) {
if (err) {
console.log(err);
return;
} else {
if ( typeof cb === "function") {
cb();
}
}
});
}
var queryData = function(req, res) {
authorize(function() {
analytics.data.ga.get({
'auth': jwtClient,
'ids': VIEW_ID,
'metrics': 'ga:uniquePageviews',
'dimensions': 'ga:pagePath',
'start-date': '30daysAgo',
'end-date': 'yesterday',
'sort': '-ga:uniquePageviews',
'max-results': 10,
}, function (err, response) {
if (err) {
console.log(err);
return;
}
res.send(response);
});
});
}
module.exports = {
queryData
};
and my express server set-up :
const app = express();
app.use('/', express.static('public'));
app.use('/', express.static('src'));
var ga = require('./src/apicall/gadata');
app.listen(process.env.PORT || 3000);
app.set('views', './src/views');
app.set('view engine', 'ejs');
app.use('/gadata', ga.queryData);
so after I've dispatched my axios.post, how do I handle it in my server.babel.js file ?
I guess I need to use a structure like this :
router.get('/newUser', (req, res) => {
TestUser.save({
name: 'sawyer',
email: 'sawyer#test.com'
}).then((result) => {
console.log('Saved!')
res.send(result)
})
})
but I don't really know how I should refactor it to pass my parameters so I can actually update my google analytics api call ?
thanks.

Nodejs random query

I'm trying to get a random item from my local database using ajax.The first time i do a ajax request i get a random item afterwards every ajax request return the same item.
var express = require('express')
var app = express();
var customers = require('./module');
var pg = require('pg');
var conString = "postgres://postgres:pass#localhost/test";
var client = new pg.Client(conString);
app.get('/res', function(req, res) {
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
client.query('SELECT * FROM t_items OFFSET random()*300 LIMIT 1', function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0]);
res.contentType('json');
res.send({ some: result.rows[0] });
client.end();
});
});
});
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.set('port', (process.env.PORT || 5000))
app.use(express.static(__dirname + '/public'))
app.get('/',function(req, res){
res.render("index");
});
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
})
If I try to wrap it in app.post('/req' )....
i get could not connect to postgres [Error: Connection terminated]
I've tried with client pooling but still the same problem
Just move your query inside function app.get('/res', function(req, res)
This happening because db query is executed only once. To prevent it - move your code inside /res route, and it will be executed every request.

Resources