1 | # mini-tools
|
2 | mini tools for express and others
|
3 |
|
4 |
|
5 | ![extending](https://img.shields.io/badge/stability-extending-orange.svg)
|
6 | [![npm-version](https://img.shields.io/npm/v/mini-tools.svg)](https://npmjs.org/package/mini-tools)
|
7 | [![downloads](https://img.shields.io/npm/dm/mini-tools.svg)](https://npmjs.org/package/mini-tools)
|
8 | [![linux](https://img.shields.io/travis/codenautas/mini-tools/master.svg)](https://travis-ci.org/codenautas/mini-tools)
|
9 | [![windows](https://ci.appveyor.com/api/projects/status/github/codenautas/mini-tools?svg=true)](https://ci.appveyor.com/project/codenautas/mini-tools)
|
10 | [![coverage](https://img.shields.io/coveralls/codenautas/mini-tools/master.svg)](https://coveralls.io/r/codenautas/mini-tools)
|
11 | [![climate](https://img.shields.io/codeclimate/github/codenautas/mini-tools.svg)](https://codeclimate.com/github/codenautas/mini-tools)
|
12 | [![dependencies](https://img.shields.io/david/codenautas/mini-tools.svg)](https://david-dm.org/codenautas/mini-tools)
|
13 |
|
14 |
|
15 | language: ![English](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-en.png)
|
16 | also available in:
|
17 | [![Spanish](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-es.png)](LEEME.md)
|
18 |
|
19 | ## Install
|
20 |
|
21 | ```sh
|
22 | $ npm install mini-tools
|
23 | ```
|
24 |
|
25 |
|
26 | ## Main goal
|
27 |
|
28 | Have some mini tools for express and others
|
29 |
|
30 |
|
31 | ## API
|
32 |
|
33 | ### serveErr(req, res [, next])
|
34 |
|
35 |
|
36 | Returns a function that sends a error message to de front-end.
|
37 |
|
38 |
|
39 | ```js
|
40 | app.post('/insert' , function(req,res){
|
41 | //...
|
42 | if(duplicate){
|
43 | serveErr(req,res)(new Error("Duplicate name. Can't insert"));
|
44 | return;
|
45 | }
|
46 | //...
|
47 | ```
|
48 |
|
49 |
|
50 | It is promise friendly
|
51 |
|
52 |
|
53 | ```js
|
54 | app.use('/tools', function(req,res,next){
|
55 | //...
|
56 | .then(function(){
|
57 | if(not_in_this_middleware){
|
58 | throw new Error("next");
|
59 | }
|
60 | // ...
|
61 | }).catch(serveErr(req,res,next));
|
62 | ```
|
63 |
|
64 |
|
65 | *catch* expects a function that receive an error.
|
66 | *serveErr* returns that function.
|
67 |
|
68 | When err is Error("next") *serveErr* calls next and does not send any result to de front-end;
|
69 | otherwise it sends a 400 error with the message and stack.
|
70 |
|
71 |
|
72 | ### serveJade(path, any)
|
73 |
|
74 | ```js
|
75 | var express = require('express');
|
76 | var app = express();
|
77 |
|
78 | app.use('/',MiniTools.serveJade('./static',true));
|
79 |
|
80 | app.use('/main',MiniTools.serveJade('./static/index.jade',false));
|
81 | ```
|
82 |
|
83 |
|
84 | Returns an express middleware to serve jade files.
|
85 | If `any==true` it serves files adding .jade to req.path; and
|
86 | if there is no jade file it call `next()`.
|
87 |
|
88 | If `any==false` it serves that specific file.
|
89 |
|
90 | **Note**: for use serveJade you must include jade in `package.json`
|
91 |
|
92 |
|
93 | ### serveStylus(path, any)
|
94 |
|
95 | ```js
|
96 | var express = require('express');
|
97 | var app = express();
|
98 |
|
99 | app.use('/',MiniTools.serveStylus('./static',true));
|
100 |
|
101 | app.use('/site.css',MiniTools.serveStylus('./static/index.styl',false));
|
102 | ```
|
103 |
|
104 |
|
105 | Returns an express middleware to serve jade files.
|
106 | If `any==true` it serves files adding .jade to req.path; and
|
107 | if there is no jade file it call `next()`.
|
108 |
|
109 | If `any==false` it serves that specific file.
|
110 |
|
111 | **Note**: for use serveStylus you must include stylus in package.json
|
112 |
|
113 |
|
114 | ## License
|
115 |
|
116 |
|
117 | [MIT](LICENSE)
|
118 |
|