mini-tools
mini tools for express and others
Last updated 5 months ago by tute .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mini-tools 
SYNC missed versions from official npm registry.

mini-tools

mini tools for express and others

extending npm-version downloads linux windows coverage climate dependencies

language: English also available in: Spanish

Install

$ npm install mini-tools

Main goal

Have some mini tools for express and others

API

serveErr(req, res [, next])

Returns a function that sends a error message to de front-end. If the error object has setted the property

  • code: is displayed before the message
  • status: is sended in the header (otherwise "400" is sended)
app.post('/insert' , function(req,res){
  //...
  if(duplicate){
    serveErr(req,res)(new Error("Duplicate name. Can't insert"));
    return;
  }
  //...

It is promise friendly

app.use('/tools', function(req,res,next){
  //...
  .then(function(){
    if(not_in_this_middleware){
      throw new Error("next");
    }
    // ...
  }).catch(serveErr(req,res,next));

catch expects a function that receive an error. serveErr returns that function.

When err is Error("next") serveErr calls next and does not send any result to de front-end; otherwise it sends a 400 error with the message and stack.

serveJade(path, opts)

var express = require('express');
var app = express();

app.use('/',MiniTools.serveJade('./static',true));

app.use('/main',MiniTools.serveJade('./static/index.jade',false));

Returns an express middleware to serve jade files.

If opts is boolean it will be the any option.

If any==true it serves files adding .jade to req.path; and if there is no jade file it call next().

If any==false it serves that specific file.

Others options in opts are pased to pug.render function.

Note: for use serveJade you must include "pug" in package.json

serveStylus(path, any)

var express = require('express');
var app = express();

app.use('/',MiniTools.serveStylus('./static',true));

app.use('/site.css',MiniTools.serveStylus('./static/index.styl',false));

Returns an express middleware to serve jade files. If any==true it serves files adding .jade to req.path; and if there is no jade file it call next().

If any==false it serves that specific file.

Note: for use serveStylus you must include stylus in package.json

serveText(anyText,contentTypeText)

var express = require('express');
var app = express();

app.use('/about',MiniTools.serveText('<h1>This app</h1>','html'));

app.use('/is-up-service',MiniTools.serveText('Yes.'));

Returns an express middleware to serve pain text. Optionaly you can pass "content type".

serveJson(object)

var express = require('express');
var app = express();

var config = {devel:false, title: "title"};

app.use('/config',MiniTools.serveJson(config));

Returns an express middleware to serve an object in JSON format.

serveYaml(object)

var express = require('express');
var app = express();

var config = {devel:false, title: "title"};

app.use('/config',MiniTools.serveYaml(config));

Returns an express middleware to serve an object in yaml format (using js-yaml).

readConfig(list, opts)

MiniTools.readConfig(
    [
        {production: true},
        'package.json',
        'other-configs.yml',
        'more-configs',
    ],
    {whenNotExist:'ignore'}
).then(function(config){
    console.log(config);
});

Reads the chain of configuration merging with best-globals.changing.

If the list element is a fileName ending with .json .yaml o .yml, it reads and parse, if doesn't have extension it search first, if it is a plain object it uses directly.

options

  • whenNotExist:'ignore'
  • whenNotExist:'fail'

License

MIT

Current Tags

  • 1.0.9                                ...           latest (19 days ago)

51 Versions

  • 1.0.9                                ...           19 days ago
  • 1.0.8                                ...           a month ago
  • 1.0.7                                ...           4 months ago
  • 1.0.6                                ...           5 months ago
  • 1.0.5                                ...           5 months ago
  • 1.0.4                                ...           9 months ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.3.22                                ...           2 years ago
  • 0.3.21                                ...           2 years ago
  • 0.3.19                                ...           2 years ago
  • 0.3.18                                ...           2 years ago
  • 0.3.17                                ...           2 years ago
  • 0.3.16                                ...           2 years ago
  • 0.3.15                                ...           2 years ago
  • 0.3.14                                ...           2 years ago
  • 0.3.13                                ...           2 years ago
  • 0.3.12                                ...           2 years ago
  • 0.3.11                                ...           2 years ago
  • 0.3.10                                ...           2 years ago
  • 0.3.9                                ...           3 years ago
  • 0.3.8                                ...           3 years ago
  • 0.3.7                                ...           3 years ago
  • 0.3.5                                ...           3 years ago
  • 0.3.4                                ...           3 years ago
  • 0.3.3                                ...           3 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.12                                ...           3 years ago
  • 0.2.11                                ...           3 years ago
  • 0.2.10                                ...           3 years ago
  • 0.2.9 [deprecated]           ...           3 years ago
  • 0.2.8 [deprecated]           ...           3 years ago
  • 0.2.7 [deprecated]           ...           3 years ago
  • 0.2.6 [deprecated]           ...           3 years ago
  • 0.2.5 [deprecated]           ...           3 years ago
  • 0.2.3 [deprecated]           ...           3 years ago
  • 0.2.1 [deprecated]           ...           3 years ago
  • 0.2.0 [deprecated]           ...           3 years ago
  • 0.1.4 [deprecated]           ...           4 years ago
  • 0.1.3 [deprecated]           ...           4 years ago
  • 0.1.2 [deprecated]           ...           4 years ago
  • 0.1.1 [deprecated]           ...           4 years ago
  • 0.1.0 [deprecated]           ...           4 years ago
  • 0.0.4 [deprecated]           ...           4 years ago
  • 0.0.3 [deprecated]           ...           4 years ago
  • 0.0.2 [deprecated]           ...           4 years ago
  • 0.0.1 [deprecated]           ...           4 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 7
Last Day 0
Last Week 6
Last Month 149
Dependencies (8)
Dev Dependencies (16)

Copyright 2014 - 2017 © taobao.org |