azure node.js express sessions stored on Azure caching - node.js

I am trying to store node.js express sessions on Azure caching.
We already set up a dedicated caching on a worker role following this article http://blogs.msdn.com/b/silverlining/archive/2013/01/03/november-updates-to-windows-azure-powershell-cmdlets.aspx
Add-AzureCacheWorkerRole mycacherole
Enable-AzureMemcacheRole mywebrole mycacherole
In the sample, they use mc module
Can you help me on how to stored express session on azure cache using that module?
I also tried connect-memcached https://github.com/balor/connect-memcached as AzureCache:
MemcachedStore = require('connect-memcached')(express);
app.use(express.session({
secret: 'CatOnTheKeyboard',
store: new MemcachedStore
}));
We got error with this trace. Is it possible to get express to store it sessions on azureCache? Thank for your help.
TypeError: Cannot call method 'reverse' of null
at Client.connect
(E:\approot\node_modules\connect-memcached\node_modules\memcached\lib\memcached.js:129:42)
at Client.memcachedCommand [as command]
(E:\approot\node_modules\connect-memcached\node_modules\memcached\lib\memcached.js:299:10)
at Client.get
(E:\approot\node_modules\connect-memcached\node_modules\memcached\lib\memcached.js:794:10)
at MemcachedStore.get
(E:\approot\node_modules\connect-memcached\lib\connect-memcached.js:82:19)
at Object.session [as handle]
(E:\approot\node_modules\connect\lib\middleware\session.js:309:11)
at next (E:\approot\node_modules\connect\lib\proto.js:190:15)
at Object.cookieParser [as handle]
(E:\approot\node_modules\connect\lib\middleware\cookieParser.js:60:5)
at next (E:\approot\node_modules\connect\lib\proto.js:190:15)
at Object.handle
(E:\approot\node_modules\express-validator\lib\express_validator.js:148:10)
at next (E:\approot\node_modules\connect\lib\proto.js:190:15)

Related

Could not connect to MongoDB! TypeError: OpenIDConnectStrategy requires an issuer option

This the replication of the same question Could not connect to MongoDb : openidconnect strategy requires an issuer option.
In local its working fine by using local MongoDB. Used Mongo URI for server setup, in server the following error received while start the meanJS project.
Could not connect to MongoDB!
TypeError: OpenIDConnectStrategy requires an issuer option
at Strategy (/project_dir/node_modules/passport-openidconnect/lib/strategy.js:31:32)
at new Strategy (/project_dir/node_modules/passport-paypal-openidconnect/lib/passport-paypal-openidconnect/strategy.js:60:25)
at module.exports (/project_dir/modules/users/server/config/strategies/paypal.js:11:16)
at /project_dir/modules/users/server/config/users.server.config.js:31:36
at Array.forEach (<anonymous>)
at module.exports (/project_dir/modules/users/server/config/users.server.config.js:30:78)
at /project_dir/config/lib/express.js:139:38
at Array.forEach (<anonymous>)
at Object.module.exports.initModulesConfiguration (/project_dir/config/lib/express.js:138:31)
at Object.module.exports.init (/project_dir/config/lib/express.js:250:8)
at /project_dir/config/lib/app.js:26:23
at /project_dir/config/lib/mongoose.js:35:21
at process._tickCallback (internal/process/next_tick.js:68:7)
There is no proper solution for this issue, kindly provide a way to get out of this issue. Thanks in advance

Layer.match is not a function

so i work at a startup building a backend web app using the nestJs framework (is slightly based on express) and typeorm for our Db . So we recently ran into an issue where the app literally crashes every time an error is thrown i.e an incorrect password
.the error below :
TypeError: layer.match is not a function
at matchLayer (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/router/index.js:574:18)
at next (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/router/index.js:220:15)
at Function.handle (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/router/index.js:174:3)
at Function.handle (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/application.js:174:10)
at Server.app (/home/imuchene/apps/DocuflowApi/node_modules/express/lib/express.js:39:9)
at Server.emit (events.js:187:15)
at Server.EventEmitter.emit (domain.js:441:20)
at parserOnIncoming (_http_server.js:672:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
now for context we're using the Error class on javascript, even though we think that this is not the issue and rather the routing mechanism of expressjs crashes after the error is thrown , also its worth noting that our application runs fine locally but once deployed on the vm it causes the issue.

Session storage and mongo connect - Can't Set Headers after they are sent

I've read all the related questions to this one on here, I've searched through Github issues and I've read as much as possible about Express to try and figure this one out. As far is I've read most people say it is something to do with the headers being set after they've been sent usually with res.set() or something similar but I'm not doing that anywhere in my code.
This error only occurs shortly after I've deployed my SailsJS app to Modulus:
Error occurred in session middleware :: 'Error: Error getting collection: sessions\n at /mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:160:19\n at Db.collection (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:524:27)\n at MongoStore._get_collection (/mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:158:17)\n at MongoStore.set (/mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:274:12)\n at Session.save (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/express-session/session/session.js:63:25)\n at ServerResponse.end (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/express-session/index.js:263:21)\n at ServerResponse.res.end (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/node_modules/compression/index.js:116:15)\n at ServerResponse.res.send (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:175:8)\n at ServerResponse.res.json (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:223:15)\n at storedUrlCheckServiceDone (/mnt/data/2/api/controllers/ProcessController.js:44:18)\n at tryCatcher (/mnt/data/2/node_modules/bluebird/js/main/util.js:24:31)\n at Promise._settlePromiseFromHandler (/mnt/data/2/node_modules/bluebird/js/main/promise.js:454:31)\n at Promise._settlePromiseAt (/mnt/data/2/node_modules/bluebird/js/main/promise.js:530:18)\n at Promise._settlePromises (/mnt/data/2/node_modules/bluebird/js/main/promise.js:646:14)\n at Async._drainQueue (/mnt/data/2/node_modules/bluebird/js/main/async.js:175:16)\n at Async._drainQueues (/mnt/data/2/node_modules/bluebird/js/main/async.js:185:10)'
http.js:690
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.res.setHeader (/mnt/data/2/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:133:22)
at ServerResponse.res.set.res.header (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:577:10)
at ServerResponse.res.send (/mnt/data/2/node_modules/sails/node_modules/express/lib/response.js:142:12)
at Object.<anonymous> (/mnt/data/2/node_modules/sails/lib/hooks/http/middleware/defaults.js:56:24)
at Object.immediate._onImmediate (timers.js:372:16)
at processImmediate [as _immediateCallback] (timers.js:354:15)
[2015-04-13T03:23:03.264Z] Application CRASH detected. Exit code 8.
It's something to do with the sessions and connect-mongo but I have no idea how to figure it out. Usually it happens once or twice straight after deployment but sometimes it'll happen up to 5 or 6 times in a row straight after deployment as well.
I've also seen this error occasionally as well around the same time so it might be related:
/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
Error: Error setting TTL index on collection : sessions
at /mnt/data/2/node_modules/connect-mongo/lib/connect-mongo.js:169:23
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1497:46
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1630:20
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:170:22
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:714:39
at Cursor.close (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:1009:5)
at commandHandler (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:714:21)
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:453:41)
at /mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:488:18
at MongoReply.parseBody (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:446:20)
at emit (events.js:95:17)
at null.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
at emit (events.js:98:17)
at Socket.<anonymous> (/mnt/data/2/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:440:22)
[2015-04-13T03:24:15.011Z] Application CRASH detected. Exit code 8.
[2015-04-13T03:24:22.048Z] Application restarted with pid 14613
I'm using Node v0.10.37, Sails v0.10.5 and Passport for authentication and just the mongo adapter for session storage.
I'd really appreciate any help or insights you can give as I've spent nearly a week trying to figure this out before posting here and even had Newrelic's support team looking at and they couldn't figure it out either.
Thanks
Update
I'm not seeing this error locally, only on Modulus.
This is a shortened down version of my controller.
I basically have a Auth Service that checks if the users is authenticated, if they are I pass the results off to another promise which checks the db for some of their data, if it find a record then it returns it if not then it calls a 3rd party API and does some processing, once thats done it returns and creates a new record, then returns the results back to the browser. If they aren't authenticated then I send back an error.
There are 4 calls to different Sails services in total in the controller, which all return a promise.
text: function (req, res) {
// Check that the host is authenticated
AuthService.hostname(req).done( authServiceDone, authServiceFail );
function authServiceDone (site) {
// Check the DB for the origin
return urlService.check(req, site.owner).done(UrlCheckServiceDone, UrlCheckServiceFail );
}
function authServiceFail (error) {
return res.json({
error : error
});
}
}

Node.js Mocha Unit Testing error re: Mongoose mocks with Mockgoose, "Error setting TTL index on collection : sessions"

Making this open source application, outrightmental/partybot-web.git on GitHub built on Daftmonk's mighty Yeoman angular-fullstack generator.
I've been trying to wrap my skills around the seemingly hot contemporary problem of doing MongoDB mocks during testing (local and CI) of Node.js.
I'm a fan of Mocha for running and Chai for assertion. After knocking out many early contenders, I'm experimenting now with Mockgoose for mocking behind Mongoose models in lieu of MongoDB.
See also the build pertaining to this issue, in my open source partybot-web CI on Travis
So far, this configuration has been able to successfully run some database operations, proving that Mockgoose is not a complete waste of time. However, this "sessions" table seems to be stumping it.
Please see example local execution below (Ubuntu 12), and let me know if any Node wizards out there have a pearl of wisdom...
Thanks very much!
-Nick
nick#om-ultrabook-ubuntu:~/Development/partybot-web$ mocha test/server/api/incoming/sms.js
info - socket.io started
Express server listening on port 3000 in development mode
Error: Error setting TTL index on collection : sessions
at /home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:161:23
at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1404:28
at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1542:30
at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:159:22
at commandHandler (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:678:48)
at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1806:9
at __executeQueryCommand (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1636:44)
at Db._executeQueryCommand (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1805:7)
at Cursor.nextObject (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:729:13)
at Cursor.toArray (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:158:10)
at Cursor.toArray (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/scope.js:10:20)
at /home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1541:65
at Db.collection (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:495:44)
at Db.indexInformation (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1539:8)
at Db.ensureIndex (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1403:8)
at Collection.ensureIndex (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/collection/index.js:65:11)
at /home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:159:29
at Db.collection (/home/nick/Development/partybot-web/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:495:44)
at MongoStore._get_collection (/home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:150:17)
at MongoStore.set (/home/nick/Development/partybot-web/node_modules/connect-mongo/lib/connect-mongo.js:262:12)
at Session.save (/home/nick/Development/partybot-web/node_modules/express/node_modules/connect/node_modules/express-session/session/session.js:63:25)
at ServerResponse.res.end (/home/nick/Development/partybot-web/node_modules/express/node_modules/connect/node_modules/express-session/index.js:304:19)
at twilioResponse (/home/nick/Development/partybot-web/lib/controllers/incoming.js:19:7)
at Promise.<anonymous> (/home/nick/Development/partybot-web/lib/controllers/incoming.js:58:5)
at Promise.<anonymous> (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
at Promise.EventEmitter.emit (events.js:98:17)
at Promise.emit (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
at Promise.fulfill (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
at handleSave (/home/nick/Development/partybot-web/node_modules/mongoose/lib/model.js:133:13)
at /home/nick/Development/partybot-web/node_modules/mongoose/lib/utils.js:408:16
at /home/nick/Development/partybot-web/node_modules/mockgoose/lib/Collection.js:116:13
at Object.validateOptions [as validate] (/home/nick/Development/partybot-web/node_modules/mockgoose/lib/validation/Validation.js:40:5)
at Collection.insert (/home/nick/Development/partybot-web/node_modules/mockgoose/lib/Collection.js:110:20)
at model.save (/home/nick/Development/partybot-web/node_modules/mongoose/lib/model.js:190:21)
at model._done (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:59:24)
at _next (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:52:28)
at fnWrapper (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:159:8)
at model.<anonymous> (/home/nick/Development/partybot-web/lib/models/message.js:31:3)
at _next (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:50:30)
at fnWrapper (/home/nick/Development/partybot-web/node_modules/mongoose/node_modules/hooks/hooks.js:159:8)
at complete (/home/nick/Development/partybot-web/node_modules/mongoose/lib/document.js:974:5)
at /home/nick/Development/partybot-web/node_modules/mongoose/lib/document.js:965:20
at ObjectId.SchemaType.doValidate (/home/nick/Development/partybot-web/node_modules/mongoose/lib/schematype.js:603:22)
at /home/nick/Development/partybot-web/node_modules/mongoose/lib/document.js:956:9
at process._tickCallback (node.js:415:13)
GET /api/incoming/sms 200 21ms
․db connection open
POST /api/incoming/sms 200 6ms
The "sessions" MongoDB collection is being requested here, by the Node.js Express framework's "sessions" module:
Session.save (node_modules/express-session/session/session.js:63:25)
I have been using Mockgoose to mock my Mongoose-wrapped MongoDB tables.. and it is doing its job perfectly.
The problem is that this "sessions" collection is being specified directly in the Express framework configuration, not implementing Mongoose:
// Persist sessions with mongoStore
app.use(express.session({
secret: 'angular-fullstack secret',
store: new mongoStore({
url: config.mongo.uri,
collection: 'sessions'
}, function () {
console.log("db connection open");
})
}));
I'll have to do my Express session mocking separately!
Not all of Mongoose is fully implemented yet. However if you can add a test case that shows this issue then I normally will focus on implementing that item over the others on my list.
https://github.com/mccormicka/Mockgoose/issues/38
I just ran into the same issue - my solution was to use express-session's memorystore option (the default when no other is specified), rather than mongodb. Really quickly, I just did this:
if (process.env.NODE_ENV === 'test') {
app.use(session({ secret: config.sessionSecret }));
} else {
app.use(session({
secret: config.sessionSecret,
store: new mongoStore({
//db: db.connection.db,
mongoose_connection: db.connection,
collection: config.sessionCollection
})
}));
}

Scaling Socket.IO and broadcast to all belonging clients

I'm using
Express with "connect-redis" session store
And I tied it with Socket.IO through configuring "authorization"
So that I don't have to actually use
socket.get
Or
socket.set
To store and retrieve a client's belonging variables.
But I'm not sure that If I broadcast a message to specific group of people, connected in different server, get message successfully.
So that it is truly scaled!
But It seems to be not truly scaled but only use Redis as storing values individually.
Also I tried to use
RedisStore
which is given by Socket.IO
does not work.
It is saying
DEBUG: TypeError: Converting circular structure to JSON
at Redis.stringify [as pack] (native)
at Redis.publish (/var/www/node_modules/socket.io/lib/stores/redis.js:106:31)
at Manager.handleClient (/var/www/node_modules/socket.io/lib/manager.js:646:18)
at Manager.handleHTTPRequest (/var/www/node_modules/socket.io/lib/manager.js:595:8)
at Manager.handleRequest (/var/www/node_modules/socket.io/lib/manager.js:557:12)
at HTTPServer.<anonymous> (/var/www/node_modules/socket.io/lib/manager.js:112:10)
at HTTPServer.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1507:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1403:22)
How can I scale Socket.IO with broadcasting to all scaled clients available?
Edit: I followed the Express-Session-Socket.IO tutorial and I have found that
io.authorization actually makes problem but it is okay without io.authorization.
How is that?
Socket.IO has his own authorization. It authorize for each socket. To customize the socket.IO Auth, using my code, write inside the io.configure function:
io.set('authorization', function (handshakeData, callback) {
//your auth logic
});
With my example RedisStore you scale to all bradcasting clients available.

Resources