mercredi 1 avril 2015

Socket.io with PhoneGap disconnects when application is in the background

I have a phonegap app (currently on Adroid only) that uses socket.io against a node.js server. Everything works fine when the application is in foreground. The problem is that when it goes to background, after 3-4 minutes I see a disconnect on the server. This happens even if from the client I send emit() while the app is in background.


I have tried all socket.io options, e.g. keepalive, timeout, etc., but nothing helped so far.


Anyone with similar problem and possible solution?


Thanks in advance!


Express redirect to front-end route with path

Let's say I have a SPA that has a route /contact


For the direct requests to Express (ie. not to index.html) I need to execute a redirect.


In Express I can define this route like this:



app.use(function(req,res,next) {
if (req.path === "/contact" ) {
return res.redirect("/?path=" + req.path);
}
next();
});


This means that if the user makes a direct request they will end up on index.html with "http://ift.tt/1xZhAbP" in their address bar. Then the front-end can take care of the routing etc.


But maybe there is a way to do this so the user does not see this ugly url?


CouchbaseError: Client-Side timeout exceeded for operation

I am running couchbase server 3.0.1, with NodeJS SDK. I have set up a server with two buckets, and no documents. I opened a connections via:


var dbCluster = new couchbase.Cluster([my_host]); var db = dbCluster.openBucket( [my_bucket_name]);


When I try to execute 'db.upsert', I get the following error:


Error: CouchbaseError: Client-Side timeout exceeded for operation. Inspect network conditions or increase the timeout


Now the strangest part, is that the document still gets added. So the command executes successfully, but still returns the error. Also the call gets executed very fast, and definitely does not take 2.5 sec (default operation timeout).


Following this: Couchbase times out after few seconds I have tried increasing the timeout via:


db.operationTimeout = 5000;


This did not work. Again the call was executed successfully, but timeout error was returned even though the specified timeout did not pass (the call was very fast, much faster than 5 sec).


The only other support for this I could find is in this Google Groups, but it seems dead: http://ift.tt/1EzGlgP


I am running OSX Yosemite 10.10.2. Any help would be really appreciated, as I do not know where to look next.


Thank you,


Here is the code for reference:



var couchbase = require('couchbase');

var dbCluster = new couchbase.Cluster("mylocalhostname");
var db = dbCluster.openBucket( "mybucketname", function(err){
// no errors here
if(err){
console.log("Can't open bucket");
throw err;
}
else {
console.log("Successfully opened bucket");
}
});

db.operationTimeout = 5000;

db.upsert("docname", someJSON, function(err, result){

if(err){
// get an error here, even though the call successfully executes
return console.log("Error: " + err);
}
});

How to make these two similar blocks codes reusable?

I have two similar codes and I think it could be reusable but not sure where to start. If you can show me and example or link me to an article that would be fine.


I have this one block that updates a user info.



exports.updateUser = function(req, res, next) {
User.findById(req.params.id, function(err, user) {
if (err) {
return next(err);
}
user.email = req.body.email || '';
user.firstname = req.body.firstname || '';
user.lastname = req.body.lastname || '';

user.save(function(err) {
if (err) {
return next(err);
}
req.flash('success', {
msg: 'User information updated.'
});
res.redirect('/users');
});
});
};


and this block that updates my user account info.



exports.updateAccount = function(req, res, next) {
User.findById(req.user.id, function(err, user) {
if (err) {
return next(err);
}
user.email = req.body.email || '';
user.firstname = req.body.firstname || '';
user.lastname = req.body.lastname || '';

user.save(function(err) {
if (err) {
return next(err);
}
req.flash('success', {
msg: 'Profile information updated.'
});
res.redirect('/account');
});
});
};


How can I make a reusable update.


Trying to use mongodump with remote server heroku using mongolab

In my computer i'm running this command to make a backup of the information of some mongodb database.



mongodump -h <hostName>:<port> -d <databes> -u <user> -p <pass> -o direction/where/to/make Backup


But gave to me this error:


Failed: error connecting to db server: no reachable servers


I try to connect robomongo with the same user, pass, host and databes and it's work fines.


Version of mongo 3.0.1


Thanks.


Node Server - res.redirect is using 'PUT' method. Need 'GET'

Using MEAN stack.


I created a mongoose call to update a document in my mongodb collection. That is working, no issue. However when the update is complete I want to "simply" redirect to my '/search' page via server controller.


The redirect works on my other mongoose save call, which is a simple 'POST'.


However when I call redirect after my 'PUT' the redirect is using PUT and not GET, resulting in a 404, that the method PUT /search can't be found. That would be correct!, however I'm not sure why its choosing 'PUT' method. Can I force a 'GET'?


My express routes are using passport, so all pages that require authentication go through my index.js. (see gist). My mongoose calls calls all go directly from my view or controller to the server via /api/... route.


Hoping this is a quick fix.


My code in a gist. http://ift.tt/1CaEJ5U


Client Controller - call to server to perform update (works)



$scope.updateProduct = function() {
//$scope.edited.inv = $scope.edited.searchSKU;
$scope.edited.inv.SKU = $scope.edited.SKU;
$scope.edited.inv.Product_Name = $scope.edited.Name;
$scope.edited.inv.Product_Description = $scope.edited.Desc;
$scope.edited.inv.Quantity = $scope.edited.quantity;
$scope.edited.inv.Product_Location = $scope.edited.locations;
//console.log('Edited: ' + JSON.stringify($scope.edited.inv));
//console.log('Not Edited: ' + JSON.stringify($scope.prod.currentProd));
var inventory = new Inventory($scope.edited.inv);
//console.log('edited.inv._id: ' + $scope.edited.inv._id);
inventory.$update({id : $scope.edited.inv._id}, inventory, function (err, results){
if(err){};
console.log(results);
});
}


Server Controller - Failing to redirect:



module.exports.editInventory = function(req, res){
Product.update({_id : req.body._id}, req.body, function (err, results){
if(err){
req.flash('message', 'Error Updating Product');
console.log('Error Updating Product: ' + err);
throw err;
}
req.flash('message','Product Updated Successfully!');
res.redirect('/search');
});


};


Let me know if you need additional info.


mardi 31 mars 2015

front end workflow tools and NODE.JS

as a example



  • package manager (BOWER,GULP)

  • JavaScript Task Runner(GRUNT)

  • web scaffolding tool (Yeoman)


why theses type of front end workflow tools ONLY AVAILABLE IN NODEJS not any other language.