Comparitively new to Node.js, can somebody tell me why I am not able to send the events to the Kafka topic? Code is not producing any errors though.
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 config = require('./config.js');
var nforce = require('nforce');
var dateTime = require('node-datetime');
var kafka = require('kafka-node');
var routes = require('./routes/index');
var dt = dateTime.create();
var org = nforce.createConnection({
clientId: config.CLIENT_ID,
clientSecret: config.CLIENT_SECRET,
redirectUri: config.CALLBACK_URL + '/oauth/_callback',
mode: 'multi',
environment: config.ENVIRONMENT // optional, sandbox or production, production default
});
HighLevelProducer = kafka.HighLevelProducer,
client = new kafka.Client('localhost:9092'),
producer = new HighLevelProducer(client),
org.authenticate({ username: config.USERNAME, password: config.PASSWORD }, function(err, oauth) {
if(err) return console.log(err);
if(!err) {
console.log('*** Successfully connected to Salesforce ***');
// add any logic to perform after login
}
// subscribe to a pushtopic
var str = org.stream({ topic: config.PUSH_TOPIC, oauth: oauth });
str.on('connect', function(){
console.log('Connected to pushtopic: ' + config.PUSH_TOPIC);
});
str.on('error', function(error) {
console.log('Error received from pushtopic: ' + error);
});
str.on('data', function(data) {
console.log('Received the following from pushtopic ---');
var dataStream1 = data['sobject'];
dataStream1['timestamp'] = dt.format('Y-m-d H:M:S');
console.log(dataStream1);
var dataStreamFinal = '[' + JSON.stringify(dataStream1) + ']';
payloads = [
{ topic: 'testing1', messages: dataStreamFinal }
];
console.log(payloads);
producer.on('ready', function () {
producer.send(payloads, function (err, data) {
console.log(data);
});
});
});
});
module.exports = {app: app, server: server};
I am using the following module of node https://github.com/SOHU-Co/kafka-node
And before that I just added my code to extract the events from the salesforce pushTopic and publishing it to the kafka is my aim.
Finally able to push the data to Kafka. Though nforce does not support replay messages feature yet.
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 config = require('./config.js');
var nforce = require('nforce');
var dateTime = require('node-datetime');
var routes = require('./routes/index');
var app = express();
var server = require('http').Server(app);
var dt = dateTime.create();
var io = require('socket.io')(server);
// get a reference to the socket once a client connects
var socket = io.sockets.on('connection', function (socket) { });
var org = nforce.createConnection({
clientId: config.CLIENT_ID,
clientSecret: config.CLIENT_SECRET,
redirectUri: config.CALLBACK_URL + '/oauth/_callback',
mode: 'multi',
environment: config.ENVIRONMENT, // optional, sandbox or production, production default
apiVersion: 'v43.0'
});
org.authenticate({ username: config.USERNAME, password: config.PASSWORD }, function(err, oauth) {
if(err) return console.log(err);
if(!err) {
console.log('*** Successfully connected to Salesforce ***');
// add any logic to perform after login
}
// subscribe to a pushtopic
var str = org.stream({ topic: config.PUSH_TOPIC, oauth: oauth });
str.on('connect', function(){
console.log('Connected to pushtopic: ' + config.PUSH_TOPIC);
});
str.on('error', function(error) {
console.log('Error received from pushtopic: ' + error);
});
str.on('data', function(data) {
console.log('Received the following from pushtopic ---');
console.log(data);
var dataStream1 = data['sobject'];
dataStream1['timestamp'] = dateTime.create().format('Y-m-d H:M:S');
var dataStreamFinal = '[' + JSON.stringify(dataStream1) + ']';
var kafka = require('kafka-node'),
HighLevelProducer = kafka.HighLevelProducer,
client = new kafka.Client(),
producer = new HighLevelProducer(client),
payloads = [
{ topic: 'testing1', messages: dataStreamFinal}
];
producer.on('ready', function () {
producer.send(payloads, function (err, data) {
console.log(data);
});
});
// emit the record to be displayed on the page
//Send to kafka topic
socket.emit('record-processed', JSON.stringify(dataStreamFinal));
});
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(function(req, res, next){
res.io = io;
next();
});
// 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: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// 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 handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = {app: app, server: server};
Related
This photo shows --ms-- message. And in client side, I get timeout error.
I am trying to figure out where it occurs and why using try-catch and console.log but I can't find any.
This is 'routes/email.js'
var express = require('express');
var router = express.Router();
var db = require('../db');
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'ssssss#gmail.com',
pass: 'SSSSSS'
}
});
router.get('/send', function(req, res, next) {
console.log('/send');
var email = decodeURI(req.query.to);
console.log(email);
var rand = Math.floor((Math.random() * 9000 + 1000));
var link="http://"+req.get("host")+"/email/verify?email="+email+"&num="+rand;
var mailOptions={
to : email,
subject : "Please confirm your Email account",
html : "Hello,<br> Please Click on the link to verify your email.<br>Click here to verify"
}
try{
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.json({result:false,message:"Incorrect email", code:404});
}else{
console.log("Message sent:\n"+JSON.stringify(response, null, 2));
try{
console.log(JSON.stringify(db, null, 2));
var sql = "INSERT INTO email_verification(email, code) VALUES(?,?)";
var input = [email, rand];
db.get().query(sql, input, function(err, result){
if(err) res.json({result:false, message:"SQL error", code:403});
res.json({result:true, message:"Please check your email.", code:100});
});
// res.json({result:false, message:"DB ERROR", code:401});
}catch(e){
console.log(e);
}
}
});
}catch(e){
console.log(e);
}
});
module.exports = router;
db.get().query() part doesn't execute at all.
This is db.js.
const mariadb = require('mariadb');
var pool;
exports.connect = function(done){
console.log("Trying to connect DB...");
pool = mariadb.createPool({
host: 'localhost',
user: 'root',
password: 'ssss',
database:"SSSS",
connectionLimit: 100
});
}
exports.get = function(){
console.log("exports.get");
return pool;
}
The console.log("exports.get"); parts works fine.
This is app.js:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
var db = require('./db');
//var passport = require('./passport');
//var auth = require('./routes/auth');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
db.connect(function(err){
console.log(err);
if(err){
console.log('Unable to connect to MariaDB.');
process.exit(1);
}
});
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', require("./routes/index"));
app.use('/email', require("./routes/email"));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(404);
});
// 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;
I just used this db.jsand app.js file from my other project and it worked fine there. But I don't know why this time I have this problem.
Which part and why I am having this problem and how can I fix it?
It'd be easier to work with node.js if I can track the error easily. Figuring out the problematic part takes lots of time. Is there any module or tool for that?
I've made an nodeJS app with express. I am able to retrieve data from google analytics, however I'm not able to show the data on the screen, since the authorization/google analytics api is asynchronous.
Can someone help me with writing a function with callback, which will update a div called '.report' index page and show the JSON inside the div.
javascripts/googleAuth.js
var google = require('googleapis');
var path = require("path");
var getJWTClient = {
authorize: function () {
var jwtClient = new google.auth.JWT(
'cboboekverkoperdashboardv2#clear-style-185306.iam.gserviceaccount.com',
path.join(__dirname, '../.', '/files/KEY.json'),
null,
['https://www.googleapis.com/auth/analytics.readonly'] //scope
);
jwtClient.authorize(function (err, tokens) {
console.log('jwtClient.authorize() started');
if (err) {
console.log('error in authorization:' + err);
return;
} else {
console.log('authorization success!');
var analytics = google.analytics('v3');
return queryData(analytics,jwtClient);
}
})
}
}
function queryData(analytics,jwtClient){
var VIEW_ID = 'ga:163754738';
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;
}
console.log(JSON.stringify(response, null, 4));
// should be able to update the .report on index page with this information.
return JSON.stringify(response, null, 4);
});
}
module.exports = getJWTClient;
My app.js
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
// 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;
index.pug
extends layout
block content
h1= title
p Welcome to #{title}
div(class='report') #{jsonReport}
Routing/index.pug
var express = require('express');
var router = express.Router();
var googleAuth = require('../public/javascripts/googleAuth.js');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Dashboard', jsonReport: googleAuth.authorize() });
});
module.exports = router;
It happens that we are working on a web page that uses a list of users (for example) that we got from a MySql on the form of an array of JSON objects, it worked (we tested it with console.log() )... but that was until I activated Angularjs on the front-end.
The code that I used on the respective files are as follow...
controller.js (angular module):
var app = angular.module("App",[]);
app.controller("AppController",function($scope, $http){
$scope.listofusers = [];
$http.get('/api/users')
.success(function(data){
$scope.listofusers = data;
console.log(data);
})
.error(function(data){
console.log("Error: " + data);
});
api.js:
router.route('/users')
.get(function(request, response) {
usuariosModel.getUsuarios(function(error, data) {
data = JSON.stringify(data);
data = JSON.parse(data);
console.log(data);
response.status(200).json(data);
});
})
.post(function(request, response) {
var datosUsuario = {
username: request.body.username,
password: request.body.password,
email: request.body.email,
permiso: request.body.permiso
};
usuariosModel.insertUsuario(datosUsuario, function(error, data) {
console.log(data);
if (data) {
response.status(200).json({
"Mensaje": "Insertado"
});
} else {
response.status(500).json({
"Mensaje": "Error"
});
}
});
});
routes.js:
var express = require('express');
var app = express();
var path = require('path');
app.get('/api/usuarios', function(req, res) {
res.render('readusers.html');
//res.sendFile(path.resolve('./views/readusers.html'));
//res.sendFile(path.join(__dirname, '../', 'views', 'readusers.html'));
});
app.get('/api/usuarios', function(req, res) {
res.render('index_users')
});
app.put('/api/usuario/:username', function(req, res) {
res.render('edit');
});
module.exports = app;
server.js:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
var mysql = require('mysql');
var config = require("./models/config.js");
var fs = require('fs'); // manejo filesync
var methodOverride = require("method-override");
var connection = mysql.createConnection(config);
connection.connect(function(error) {
if (error) {
console.error('Error connecting: ' + error.stack);
return;
}
console.log('Connected to server with thread ID ' + connection.threadId);
});
// DB structure
sql_structure = fs.readFileSync('./models/bd.sql').toString();
connection.query(sql_structure, function(error, rows) {
if (error) throw error;
console.log('Base de datos: Estructura completada');
});
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
//app.set("view engine", "html");
//app.use(express.static(__dirname + '/views'));
//app.engine('html', require('ejs').renderFile);
app.use(methodOverride("_method"));
app.use('/', require('./router/routes'));
app.use(express.static(__dirname + '/views'));
//Express
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//Routes
app.use('/api', require('./router/api'));
app.listen(3000, function() {
console.log("Server on");
});
Hope someone finds what is missing, we have looked through a lot of tutorials and still can't find the mistake.
I wrote a script which save an object to a MongoDB database using mongoose. The object is correctly save to the database, but the server crashes right after, throwing me the following error message: catch(err) { process.nextTick(function() { throw err}); }
Here's a part of my code:
users.js
var User = require('../models/user');
router.post('/register', function(req, res, next) {
[...]
// checks for errors
var errors = req.validationErrors();
if (errors) {
res.render('register', {
errors: errors,
[...]
})
} else {
var newUser = new User({
[...]
});
// Create user
User.createUser(newUser, function(err, user) {
if (error) {
throw err;
};
console.log(user);
});
[...]
}
});
module.exports = router;
user.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/nodeauth');
// User Schema
var UserSchema = mongoose.Schema({
[...]
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.createUser = function(newUser, callback) {
newUser.save(callback);
}
apps.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var multer = require('multer');
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;
var expressValidator = require('express-validator');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// handle file uploads
app.use(multer({dest:'./uploads'}).single('singleInputFileName'));
// 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: false }));
// handle express session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
// passport
app.use(passport.initialize());
app.use(passport.session());
// validator
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// connect flash
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
app.use('/', routes);
app.use('/users', users);
// 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 handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
You shouldn't throw error you should return it in handler to let clients know if there is a database error creating the user. Only throw when caller can catch error, async code cannot be caught easily. Promises enable throwing errors but not callbacks.
I am creating a single page application using JavaScript(JQuery) and need to store large video files which size exceed 16Mb. I found that need to use GridFS supporting large files. As I am the new one to MongoDB I am not sure how to use GridFS. There are some good tutorials on creating applications using Node.js, MongoDB and Express but cant find any describing how to use GridFS with MongoDB (not mongoose), Express and Node.js. I managed to put up stuff for uploading files in the BSON-document size limit of 16MB. This is what I have:
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var monk = require('monk');
var Grid = require('gridfs-stream');
var db = monk('localhost:27017/elearning');
var gfs = Grid(db, mongo);
var routes = require('./routes/index');
var users = require('./routes/users');
var courses = require('./routes/courses');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Make our db accessible to our router
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/users', users);
app.use('/courses', courses);
// 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 handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
And, for example, the courses file is as the following:
var express = require('express');
var router = express.Router();
/* GET courses listing */
router.get('/courselist', function(req, res) {
var db = req.db;
var collection = db.get('courselist');
collection.find({},{},function(e,docs){
res.json(docs);
})
});
/* POST courses data */
router.post('/courselist', function(req, res) {
var db = req.db;
var collection = db.get('courselist');
collection.insert(req.body, function(err, result){
res.send(
(err === null) ? { msg: '' } : { msg: err }
);
});
});
/* Delete courses data */
router.delete('/courselist/:id', function(req, res) {
var db = req.db;
var collection = db.get('courselist');
var userToDelete = req.params.id;
collection.remove({ '_id' : userToDelete }, function(err) {
res.send((err === null) ? { msg: '' } : { msg:'error: ' + err });
});
});
module.exports = router;
I would be extremely grateful for your help, if you could tell how should I edit above files in order to utilize GridFS and be able to get, upload and delete video and picture files from my elearning database.
You can do direct uploading without using mongoose using gridfs-stream as simple as:
var express = require('express'),
mongo = require('mongodb'),
Grid = require('gridfs-stream'),
db = new mongo.Db('node-cheat-db', new mongo.Server("localhost", 27017)),
gfs = Grid(db, mongo),
app = express();
db.open(function (err) {
if (err) return handleError(err);
var gfs = Grid(db, mongo);
console.log('All set! Start uploading :)');
});
//POST http://localhost:3000/file
app.post('/file', function (req, res) {
var writeStream = gfs.createWriteStream({
filename: 'file_name_here'
});
writeStream.on('close', function (file) {
res.send(`File has been uploaded ${file._id}`);
});
req.pipe(writeStream);
});
//GET http://localhost:3000/file/[mongo_id_of_file_here]
app.get('/file/:fileId', function (req, res) {
gfs.createReadStream({
_id: req.params.fileId // or provide filename: 'file_name_here'
}).pipe(res);
});
app.listen(process.env.PORT || 3000);
for complete files and running project:
Clone node-cheat direct_upload_gridfs, run node app followed by npm install express mongodb gridfs-stream.
OR
Follow baby steps at Node-Cheat Direct Upload via GridFS README.md
Very late but I found previous answer outdated. I'm posting this because this might help newbies like me. To run it, please follow previous answers guide. All credit goes to #ZeeshanHassanMemon.
var express = require('express'),
mongoose = require('mongoose'),
Grid = require('gridfs-stream'),
app = express();
Grid.mongo = mongoose.mongo;
conn = mongoose.createConnection('mongodb://localhost/node-cheat-db');
conn.once('open', function () {
var gfs = Grid(conn.db);
app.set('gridfs', gfs);
console.log('all set');
});
//POST http://localhost:3000/file
app.post('/file', function (req, res) {
var gridfs = app.get('gridfs');
var writeStream = gridfs.createWriteStream({
filename: 'file_name_here'
});
writeStream.on('close', function (file) {
res.send(`File has been uploaded ${file._id}`);
});
req.pipe(writeStream);
});
//GET http://localhost:3000/file/[mongo_id_of_file_here]
app.get('/file/:fileId', function (req, res) {
var gridfs = app.get('gridfs');
gridfs.createReadStream({
_id: req.params.fileId // or provide filename: 'file_name_here'
}).pipe(res);
});
app.listen(process.env.PORT || 3000);