DailyJS

Node Roundup: Express Form, streamline.js, long-stack-traces

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

server node express async

Node Roundup: Express Form, streamline.js, long-stack-traces

Posted by Alex R. Young on .
Featured

server node express async

Node Roundup: Express Form, streamline.js, long-stack-traces

Posted by Alex R. Young on .

Welcome to the Node Roundup. Send in your apps and libraries using our
contact form or @dailyjs.

Express Form

Express Form by Dan Dean is a middleware data filtering and validation library for Express. It looks
like this:

app.post(
  // Route
  '/user',

  // Form filter and validation middleware
  form(
    filter("username").trim(),
    validate("username").required().is(/^[a-z]+$/),
    filter("password").trim(),
    validate("password").required().is(/^[0-9]+$/)
  ),

  // Express request-handler now receives filtered and validated data
  function(req, res){
    if (!req.form.isValid) {
      // Handle errors
      console.log(req.form.errors);

    } else {
      // Or, use filtered form data from the form object:
      console.log("Username:", req.form.username);
      console.log("Password:", req.form.password);
    }
  }
);

The form method is the middleware which will add
req.form.errors and req.form.isValid. I
haven't actually used this yet but validation at the middleware layer is
very intriguing.

streamline.js

streamline.js released by Sage is a kind of pre-processing library for Node that makes asynchronous code look synchronous.

The example given in the documentation of asynchronous API usage is
asynchronous Node file system code (which could of course be written
with the synchronous methods anyway):

function fileLength(path, callback) {
  fs.stat(path, function(err, stat) {
    if (err) { callback(err); return; }
    if (stat.isFile()) {
      fs.readFile(path, function(err, data) {
        if (err) { callback(err); return; }
        callback(null, data.length);
      });
    } else {
      callback(new Error(path + " is not a file"));
    }
  });
}

The streamline.js version uses the underscore symbol as a plug for
values that will be available later, so the previous example could be
written like this:

function fileLength(_, path) {
  if (fs.stat(path, _).isFile())
    return fs.readFile(path, _).length;
  else
    throw new Error(path + " is not a file");
}

If you're lost in callbacks and find promises and futures unsatisfactory
you might want to try this library. It's got a lot of examples and more
features for parallelizing streamlined code.

long-stack-traces

long-stack-traces by Tom Robinson uses the V8 stack track
API
to
generate long stack traces in Node or a Chromium-based browser. It's
available through npm as long-stack-traces.

Just loading the library will make it work in Node, so this
long-stack-trace example
will produce:

 
\
Uncaught Error: readFile\
at Timer.secondTimeout (/Users/alex/example.js:6:15)\ at node.js:772:9