sails.js

How can I wrap each API response into a standard reply object in SailsJS?

妖精的绣舞 提交于 2019-12-05 05:26:34
I'm new to Sails and I'm trying to figure out the best/proper method for returning a standard object for every API response. The container our front-end requires is: { "success": true/false, "session": true/false, "errors": [], "payload": [] } Currently, I’m overwriting the blueprint actions in each controller like this example (which just seems so very, very wrong): find : function( req, res ){ var id = req.param( 'id' ); Foo.findOne( { id : id } ).exec( function( err, aFoo ){ res.json( AppSvc.jsonReply( req, [], aFoo ), 200 ); }); } And in AppSvc.js: jsonReply : function( req, errors, data )

Sails.js + Passport.js getting current logged in user

白昼怎懂夜的黑 提交于 2019-12-05 05:08:35
I have a sails app that I'm working with and I'm using passport.js for user auth. I have login working, along with policies that protect my pages, all systems go. On one of my pages the user is inputting information, so I need to get the current logged in user's id. I struggled with this but eventually came up with using a route In the controller $scope.$on('sailsSocket:connect', function(ev, data) { sailsSocket.get( '/get_user', {}, function(response) { $scope.team_id = response.user; sailsSocket.get( '/status_update?sort=createdAt%20ASC&team_id='+$scope.team_id, {}, function(response) {

how to access assets/images from the view in Sails.js?

你说的曾经没有我的故事 提交于 2019-12-05 04:07:25
I simply want to add an image in a view in my Sails-Project My view-file has the location views/album/albums.ejs and the image is located in assets/images/placeholder.png if I add the image like this <img src="../../assets/images/placeholder.png"> I get this error GET http://localhost:1337/assets/images/placeholder.png 404 (Not Found) Am I missing something? Sails use Grunt (Gruntfile.js in root of project) to execute some tasks during sails lift. One of that tasks is to copy files from assets directory to .tmp/public/ directory (in development version). So if u add your file to assets

How to create Socket.io client in Python to talk to a Sails server

不想你离开。 提交于 2019-12-05 03:40:23
I am running a SailsJS instance (v0.12.3) for which I have a controller MyModelController handling a WebSocket ( socket.io ) connection that is allowed if the user has authenticated. MyModelController module.exports = { /** * Socket connection */ connect: function(req, res) { /* Checks it's a socket connection request */ if (!req.isSocket) { return res.badRequest();} /* Checks it's authenticated */ if (req.session.me) { User.findOne(req.session.me, function (err, me) { if (req.param('name')) { MyModel.findOne({ name: req.param('name') }).exec(function(err, objectFound) { /* e.g. Join a room

Should I learn Express.js or Sails.js? [closed]

痴心易碎 提交于 2019-12-05 02:27:48
I am learning node.js these days. I have gone through some nodeJS framework as well like expressJS and sailsJS but not able to decide which framework should I choose? I suggest to use sails.js , as it has better MVC structure and If you take Sails.js, then you are still on Express. It also give you a base architecture and good start points for your projects. Better go through this link and decide : http://www.quora.com/Should-one-learn-Express-js-or-Sails-js You need to be at least familar with Express.js - it's like a standart now. I would also suggest you to use sails.js as Sails makes it

How can I include javascript assets selectively in SailsJS?

泄露秘密 提交于 2019-12-05 02:07:59
问题 In a Sails.js application, how can I include javascript assets selectively? For instance, if I have an admin page and admin.js lives inside the assets/js directory. How do I keep the admin.js from loading on the public index page? I'm aware that I could move the js out to the public directory, and include the script in my admin view's template. But I'm still unable to include it after the assets.js() call inserts it's javascript. I need it to be inserted after the sails.io.js script is loaded

Webworker-threads: is it OK to use “require” inside worker?

偶尔善良 提交于 2019-12-05 01:44:15
(Using Sails.js) I am testing webworker-threads ( https://www.npmjs.com/package/webworker-threads ) for long running processes on Node and the following example looks good: var Worker = require('webworker-threads').Worker; var fibo = new Worker(function() { function fibo (n) { return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; } this.onmessage = function (event) { try{ postMessage(fibo(event.data)); }catch (e){ console.log(e); } } }); fibo.onmessage = function (event) { //my return callback }; fibo.postMessage(40); But as soon as I add any code to query Mongodb, it throws an exception: (not using

How can I add an instance method to all Models in sails.js?

好久不见. 提交于 2019-12-05 01:31:41
I'd like to add a default toDisplay function to all models which will use metadata, not unlike attribute/association definitions, to perform manipulations on the instance's attributes/associations making them suitable for display in the UI. for example: Foo.findOne(someId) .exec(function(err, foo) { ... res.view({ foo: foo.toDisplay(), }); }); So, I'd like to add this function too all models. I can imagine a Model.prototype.toDisplay = ... solution, but I'm not sure where to get Model from (some long require('waterline/..../model') path?), and if I had Model, where to put that snip-it. Please

Uploading files using Skipper with Sails.js v0.10 - how to retrieve new file name

会有一股神秘感。 提交于 2019-12-05 00:54:28
问题 I am upgrading to Sails.js version 0.10 and now need to use Skipper to manage my file uploads. When I upload a file I generate a new name for it using a UUID, and save it in the public/files/ folder (this will change when I've got this all working but it's good for testing right now) I save the original name, and the uploaded name + path into a Mongo database. This was all quite straightforward under Sails v0.9.x but using Skipper I can't figure out how to read the new file name and path.

Get sails request target from policy scope

為{幸葍}努か 提交于 2019-12-05 00:53:53
问题 I currently try to set up a simple authorization system (authentication already in place) for a sails (v0.10) application based on the policy system sails provides. For this purpose I'd need to get the controller and action the current request targets from within my policy. I'd do roughly do the following in the policy: module.exports = function (req, res, next) { // Get target controller and action var target = req.target; // How to do this? var action = req.action; // and this? // Lookup in