EzDevInfo.com

flatiron

framework components for node.js and the browser Flatiron, A framework for Node.js

Can I dump the current nconf configuration to an object?

Is there any method in nconf to collect all the keys from all the stores into a single object?

Imagine I've got this little script:

assert = require('assert');
nconf = require('nconf');

nconf.argv().env().defaults({'C': 3});
assert.equal(nconf.get('A'), 1);
assert.equal(nconf.get('B'), 2);
assert.equal(nconf.get('C'), 3);
assert.deepEqual({'A':1, 'B':2, 'C':3}, nconf.X); // <-- What's X?

that I run with

A=1 node script.js -B 2

Is there an nconf.X that will pass the test? I'd even settle for listing all the configured keys.


Source: (StackOverflow)

flatiron implementing WP style plates

How would I insert my navigation into my html file as following. (sort of wordpress style)

home.html:

<html>
 <body>
   (I dont know what to put here for nav)

   <div main>
   </div>
 </body>
<html>

nav.html

<nav>
 <img scr="logo.png">
 <ul>
  <li>home</li>
 </ul>
</nav>

And then how do I implement the template on the with plates?

Note: I'm using flatiron,plates,director


Source: (StackOverflow)

Advertisements

Node.js: Winston: Can I add default meta data to all log messages

I'm using Winston in Node.js for logging. I know I can add metadata individually to each log message but is there a way to specify a default set of metadata that'll be added to every log message (such as the app name) as I don't want to specify it every time I need to send a log message.


Source: (StackOverflow)

Can I use Flatiron's resourceful in express?

Is it possible to use Flatiron's resourcefull (ODM) in express.js?


Source: (StackOverflow)

Serving static assets via flatironjs

Is there a built-in way for serving static assets (images/css/js) via flatironjs? Or do we need to write custom routes to do this? Couldn't find any examples for any built-in way.


Source: (StackOverflow)

Node.js Flatiron HTTPS server

Is there a way of using HTTPS connection with Flatiron framework?

Updated: HTTPS server example is available on github now.


Source: (StackOverflow)

Two or more static dirs with flatiron

I was using express for a long time now. For the next project I need to use flatiron.js.

As a second dependency I use bower for JavaScript lib handling. Now I want to serve my public/ directory and the bower components/ directory to the frontend.

I tried some static file serving libraries, but all off them only serving the fist set directory. I was used to express setting multiple middlewares like this:

app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/components'));

Do I have the chance to get this running in flatiron?


Source: (StackOverflow)

Winston: how to rotate logs

How can I rotate logs when using Winston to handle logging for node.js. That is, how can I create a new file for each day the app runs?

    var logger = new (winston.Logger)({
       transports: [
          new (winston.transports.Console)(),
          new (winston.transports.File)({ filename: '2012-07-09.log' })
      ]
});

logger.log('info', 'Test Log Message', { anything: 'This is metadata' });

Source: (StackOverflow)

flatiron.js/plates partial templates?

So, I just started working with flatironjs and "plates". I'm trying to figure out how I can have a main layout template and then a partial template that loads content into the main layout template similar to how expressjs does it...

With expressjs there's the layout.js and perhaps index.js. index.js populates the content area of layout.js. It seems like this would be baked I'm not seeing a way to do this based on the documentation.


Source: (StackOverflow)

flatiron.js / plates - How to work with templates and i18n?

I just started looking at plates, as many people are talking about it.

There are some examples for plates with little html snippets, but not really a full-blown template file. So I am wondering how I can separate especially the layout into a layout.html file and the content distributed into several content.html files?

Also, I'd like to know if there are some strategies for multi-language-sites in flatiron.js/plates?

Thanks!


Source: (StackOverflow)

Node.js Express vs. Flatiron

Akin to this question, "I'm looking for the pros and cons of each framework and why one is particularly useful over the other" (but mostly what Flatiron has to offer, due to the fact that Express is already detailed pretty well in that question).

From my slight experience with Express, it seems to cover just about what you need and no more. Flatiron seems to do that, but much more minimalistically. If you check their website, you see that they offer around 5-7 main functionalities, compared to the many others included in Express.

Finally, which seems the most promising for a highly-scalable web app(s), and why should I use this or that framework over not using a framework at all?


Source: (StackOverflow)

How can I use director as router in expressjs

I want to use express.js with Flatiron's director (router) and Resourceful (ODM) because I need like the benefits of routing tables and clean multi-db schemas with validation. The reason why I now completly switch to Flatiron is, is because I think it needs some more time and there is not much doc material.

However, that is the current way I use director in express:

var express = require('express')
  , director = require('director');

function hello(){
    console.log('Success');
}

var router = new director.http.Router({
    '/': {
        get: hello
    }
});

Unfortunatly this doesn't work and gives me just a "Cannot GET /"

So what's to do?


Source: (StackOverflow)

Is there a module to easily mock req/res objects for unit testing a connect style handler?

I'm writing an app in node.js that includes some connect style endpoint handlers ( function(req,resp) ) and would like to write some unit tests against them without requiring the full app to be running.

I know I can "simply" push whatever fixture I manually write into it, but I was wondering if there is any library out there to help me generate these fixtures faster.

EDIT: to further explain what i want, I would like to in my unit test execute my handler only (not my app) and for that, i'd need a fake req and res. Those are the 2 objects I'd like to mock.

I'm currently using mocha as the test runner and the core assert module.


Source: (StackOverflow)

flatiron.js routing and templating with union, director and plates?

Coming from express.js, I want to give flatiron a try for a small project. However, there are some small problems which keep me from actually getting somewhere.

var flatiron = require('flatiron')
,  session = require('connect').session
,  ecstatic = require('ecstatic')
,  path = require('path')
,  fs = require('fs')
,  plates = require('plates')
,  director = require('director')
,  winston = require('winston')
,  union = require('union');

var router = new director.http.Router();
var server = union.createServer({
  before: [
    ecstatic(__dirname + '/public')
  ]
});

router.get('/', function () {
  var self = this;
  fs.readFile('public/layout.html', 'utf-8', function(err, html) {
    [...]
  })
});

server.listen(3000, function () {
  console.log('Application is now started on port 3000');
});

How does routing with director work? When I leave ecstatic out, I can define routes like '/' and it works, but then I don't get static CSS and JS content. With ecstatic / is replaced with 'index.html' and ecstatic has priority over all defined routes. - It's the same behavior with connect-static. Route (/) is replaced by index.html.

I also tried a different approach using the connect middleware, which doesn't work:

var flatiron = require('flatiron')
,  connect = require('connect')
,  path = require('path')
,  fs = require('fs')
,  plates = require('plates')
,  app = flatiron.app;

app.use(flatiron.plugins.http);
app.use(connect.favicon());
app.use(connect.static(__dirname + '/public'));
app.use(connect.directory(__dirname + '/public'));
app.use(connect.cookieParser('my secret here'));
app.use(connect.session({'secret': 'keyboard cat'}));

app.router.get('/', function () {
  console.log("GET /");
  var self = this;
  fs.readFile('public/layout.html', 'utf-8', function(err, html) {
    [...]
  })
});

app.listen(3000, function () {
  console.log('Application is now started on port 3000');
});

Source: (StackOverflow)

Authentication and authorization with Flatiron's Resourceful & Restful

I want to implement authentication and authorization in the Flatiron stack (using Flatiron, Resourceful and Restful). I want to require that a user has the necessary permissions, when trying to change a resource. In the Restful Readme file, there's a note about authorization:

There are several ways to provide security and authorization for accessing resource methods exposed with restful. The recommended pattern for authorization is to use resourceful's ability for before and after hooks. In these hooks, you can add additional business logic to restrict access to the resource's methods.

It is not recommended to place authorization logic in the routing layer, as in an ideal world the router will be a reflected interface of the resource. In theory, the security of the router itself should be somewhat irrelevant since the resource could have multiple reflected interfaces that all required the same business logic.

TL;DR; For security and authorization, you should use resourceful's before and after hooks.

So authorization can be handled by Resourceful's hooking system.

My actual problem is the authentication process at the beginning of every HTTP request.

Let's say I have a resource Post, a User and a resource Session. The REST API is being defined by using Restful. My main concern for this question is to ensure that a user has a session when creating a post. Other methods like save, update or for other resources like creating a user should work analogous.

File app.js:

var flatiron = require('flatiron');
var app = flatiron.app;

app.resources = require('./resources.js');

app.use(flatiron.plugins.http);
app.use(restful);
app.start(8080, function(){
  console.log('http server started on port 8080');
});

File resources.js:

var resourceful = require('resourceful');

var resources = exports;

resources.User = resourceful.define('user', function() {
  this.restful = true;
  this.string('name');
  this.string('password');
});

resources.Session = resourceful.define('session', function() {
  // note: this is not restful
  this.use('memory');
  this.string('session_id');
});

resources.Post = resourceful.define('post', function() {
  this.restful = true;
  this.use('memory');
  this.string('title');
  this.string('content');
});

resources.Post.before('create', function authorization(post, callback) {
  // What should happen here?
  // How do I ensure, a user has a session id?

  callback();
});

There's also a runnable version of the code (thanks @generalhenry).

So assume a user trying to create a post, already has been given a session id, that is sent with every request he makes by a cookie header. How can I access that cookie in the before hook (i.e. the authorization callback)?

The example can be started with node app.js and HTTP requests can be made using curl.


Source: (StackOverflow)