using connect-flash with sails.js - sails.js

I am trying to use connect-flash with sails.js, according to middleware documentation of sailsjs http://sailsjs.org/documentation/concepts/middleware , this is what I did
passportInit : require('passport').initialize(),
passportSession : require('passport').session(),
flash : require('connect-flash'),
order: [
'startRequestTimer',
'cookieParser',
'session',
'passportInit',
'passportSession',
'flash',
'myRequestLogger',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
]
and this is how my AuthController.js looks ( as I am using connect-flash with passport )
module.exports = {
_config: {
actions: false,
shortcuts: false,
rest: false
},
'login': function(req, res,next) {
passport.authenticate('local', { successRedirect: '/user/dashboard',
failureRedirect: '/login',
failureFlash: true })(res,req,next);
},
'logout': function(req, res) {
req.logout();
res.redirect('/');
}
};
But after adding flash in the queue pages never finish loading and it never renders. Please can anybody assist me with this? I am not sure how to make this work. Thanks in advance.

I've had more success with Express' flash module.
In my config/http.js:
middleware : {
flash : require('flash')(),
...
order : [
...
'passportInit',
'passportSession',
'flash',
'bodyParser',
...
],
...
}

I spent quite some time and was not able to get this package working with sales, I am sure I must be missing something, However I found another package sails-hook-flash that is plug and play for sails app.

Related

Integration of Ltijs into existing sails app

I want to use the Ltijs library (https://cvmcosta.me/ltijs) in our Sails application.
The way to deploay Ltijs as part of another express server is this (from Ltijs documentation):
const app = express()
lti.setup('EXAMPLEKEY', { url: 'mongodb://localhost/database' })
// Start LTI provider in serverless mode
await lti.deploy({ serverless: true })
// Mount Ltijs express app into preexisting express app with /lti prefix
app.use('/lti', lti.app)
The way to put middleware into Sails is something like that (without app.use(...)!):
// config/http.js
const lti = require('ltijs').Provider;
lti.setup(
'8swieleivBef',
{url: 'mongodb://localhost:27017/mysailsapp'},
);
lti.deploy({serverless: true});
module.exports.http = {
middleware: {
order: [
'cookieParser',
'session',
'bodyParser',
'ltiExpressAdapter', //<-------- my middleware adapter -----------------
'compress',
'poweredBy',
'router',
'www',
'favicon',
],
ltiExpressAdapter: lti.app, //<-------- my middleware adapter -----------------
.
.
.
The latter works, but it works to "good", because now every request is caught by Ltijs and the application doesn't work anymore.
My Question is, how do I bring the path '/lti' from app.use('/lti', lti.app) into the sails game?
I tried lots of things like this that didn't work:
ltiExpressAdapter: (function () {
return async function (req, res, next) {
if (req.path.match(/^\/lti.*$/)) {
return lti.app;
}
return next();
};
})(),
Thanks in advance for help!
Seems I found a solution using app.use(...):
// config/http.js
const express = require('express'); //<------ NEW -----
const app = express(); //<------ NEW -----
const lti = require('ltijs').Provider;
lti.setup(
'8swieleivBef',
{url: 'mongodb://localhost:27017/ltijsdb'}, //<------ (NEW) -----
);
lti.deploy({serverless: true});
module.exports.http = {
middleware: {
order: [
'cookieParser',
'session',
'bodyParser',
'ltiExpressAdapter', //<-------- my middleware adapter -----------------
'compress',
'poweredBy',
'router',
'www',
'favicon',
],
ltiExpressAdapter: app.use('/lti', lti.app), //<------ NEW -----
.
.
.
Now I get the expexted error message from Ltijs only when I call the /lti path (http://localhost:1337/lti) and the rest of the application runs like before.
Now I hopefully can go on setting up Ltijs and try to connect from a test consumer.
(I also changed the Ltijs DB so that it isn't mixed up with my App's DB.)

How to make local connections to crossbrowsertesting.com with Protractor?

How can I test my Angular Pages with Protractor in my local network at crosbrowsertesting.com? I installed "npm i cbt_tunnels" and my protractor.conf looks like this:
const cbt = require('cbt_tunnels');
export.config= {
baseUrl: 'http://localhost:4200/',
framework: 'jasmine',
directConnect: false,
seleniumAddress: 'http://<myusername>:<mykey>#hub.crossbrowsertesting.com:80/wd/hub',
capabilities : {
name : 'protractor test', // this will show up in the UI
// these are important :)
browserName : "firefox",
browser_api_name : 'FF39', // change this according to what browser you are using
os_api_name : 'Win10', // change this for the OS you are using
screen_resolution : '1024x768', // change this for the resolution
record_video : 'true',
record_network : 'true',
record_snapshot : 'true',
acceptInsecureCerts: 'true',
tunnel: 'true'
},
onComplete: () => {
browser.quit();
},
onPrepare() {
cbt.start({"username": "<myusername>", "authkey":
"<mykey>"}, function (err) {
if (!err) console.log("cbt success");
});
}
I can see the test running at crossbrowsertesting.com but the browser there says:
waiting for localhost
What is missing?
As the commenter noted, you need to start the local connection before you can actually use the local connection feature.
In this case, you will want to use this line:
'cbt.start({"username":"USERNAME","authkey":"AUTHKEY"},function(err){ if(!err) do stuff })'
from the documentation; this will allow you to automatically start the test once the local connection has been set up correctly.
In this case, do stuff is everything to run your tests (scaffolding/setup can be done externally).
Something like this is what you're really after
const cbt = require('cbt_tunnels');
cbt.start({"username":"USERNAME","authkey":"AUTHKEY"},
function(err){
if(!err) do stuff
});
Edit:
It looks like you want to start the tunnel in beforeLaunch, instead of in onPrepare, and it needs to be set as a promise. Something like this:
beforeLaunch: () => {
return new Promise( (resolve, reject) => {
cbt.start({"username": "<your email here>", "authkey": "<your auth here>"}, function (err) {
if (!err) {
console.log("cbt success");
return resolve();
}
return reject(err);
});
})
}

Sails.js change bodyParser for specific paths

In Sails.js 0.10.5, I want to replace bodyParser for specific paths. For example, use a different body parser for path '/app/upload' and for the rest use the default. How do I do this?
You can do this by overriding config/http.js. Add your custom parser to the middleware, and replace bodyParser in the order with your custom parser.
Something like this should work
module.exports.http = {
middleware: {
superBodyParser: function (req, res, next) {
if (req.path === '/app/upload') {
// your custom parser
}
else {
require('skipper')(req, res, next);
}
},
order: [
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
// 'bodyParser', <-- not required anymore
'superBodyParser'
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
]
}
};

Integrating opbeat with sails

I'm trying to integrate opbeat with sails.js. They have a node.js client which includes middleware support for Connect and Express.
I've tried to create a custom middleware in http.js
module.exports.http = {
middleware: {
opbeat : require('opbeat')({
organizationId: '...',
appId: '...',
secretToken: '...'
}),
order: [
'opbeat',
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
],
}
};
Unfortunately it doesn't work. If you could please help point me in the right direction.
The value returned from the initializer function isn't a proper middleware function (it's just an Opbeat client). To get the middleware function, call middleware.connect() on the client:
var opbeat = require('opbeat')({
organizationId: '...',
appId: '...',
secretToken: '...'
})
module.exports.http = {
middleware: {
opbeat: opbeat.middleware.connect(), // get the Opbeat middleware function
order: [
... // put the bulk of your middleware here
'opbeat'
]
}
}
P.S. The function is called middleware.connect() because it was the connect module that set the standard of having a middleware function that expects the 3 arguments; Request, Response and Callback. An alias exists that is called middleware.express() - but it's just that: An alias.
Update:
I reversed the order of the middleware in the example above so that Opbeat is placed at the end. This is important to that that it can catch errors tickling down the middleware-chain.

I get an error --- Error: [$injector:nomod] http://errors.angularjs.org/1.3.15/$injector/nomod?p0=decisionTree when I run karma.cong.js in Webstorm

Here is my karma.conf.js file
module.exports = function(config) {
var configuration = {
basePath : '',
autoWatch : false,
frameworks: ['jasmine'],
files: [
'bower_components/angular/angular.min.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-resource/angular-resource.min.js',
'bower_components/angular-animate/angular-animate.min.js',
'bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
'bower_components/angular-cookies/angular-cookies.min.js',
'bower_components/angular-sanitize/angular-sanitize.min.js',
'bower_components/angular-touch/angular-touch.min.js',
'bower_components/angular-ui-router/release/angular-ui-router.min.js',
'src/app/**/*.js'
],
ngHtml2JsPreprocessor: {
stripPrefix: 'src/',
moduleName: 'gulpAngular'
},
browsers : ['PhantomJS'],
plugins : [
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
preprocessors: {
'src/**/*.html': ['ng-html2js']
}
};
config.set(configuration);
};
I have searched such problems on stackover for a whole afternoon today, but I still can't get any solution for that.
I wonder anyone who can solve this problem.
Seems your specs are loaded before application files, and your angular module can't be resolved. Please try changing 'files' as follows:
files: [
'bower_components/jquery/dist/jquery.js',
'bower_components/angular/angular.js',
'bower_components/angular-animate/angular-animate.js',
'bower_components/angular-cookies/angular-cookies.js',
'bower_components/angular-touch/angular-touch.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-ui-router/release/angular-ui-router.js',
'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'bower_components/angular-mocks/angular-mocks.js',
'src/app/index.js',
'src/app/main/main.controller.js',
'src/app/components/navbar/navbar.controller.js',
'src/**/*.html',
'src/**/*.spec.js',
'src/**/*.mock.js'
],
karma tests run fine for me after changing the config this way