1 |
|
2 | # mini-tools
|
3 |
|
4 | algunas herramientas para express
|
5 |
|
6 |
|
7 | mini tools for express and others
|
8 |
|
9 | [!--lang:*-->
|
10 |
|
11 |
|
12 | ![extending](https://img.shields.io/badge/stability-extending-orange.svg)
|
13 | [![npm-version](https://img.shields.io/npm/v/mini-tools.svg)](https://npmjs.org/package/mini-tools)
|
14 | [![downloads](https://img.shields.io/npm/dm/mini-tools.svg)](https://npmjs.org/package/mini-tools)
|
15 | [![linux](https://img.shields.io/travis/codenautas/mini-tools/master.svg)](https://travis-ci.org/codenautas/mini-tools)
|
16 | [![windows](https://ci.appveyor.com/api/projects/status/github/codenautas/mini-tools?svg=true)](https://ci.appveyor.com/project/codenautas/mini-tools)
|
17 | [![coverage](https://img.shields.io/coveralls/codenautas/mini-tools/master.svg)](https://coveralls.io/r/codenautas/mini-tools)
|
18 | [![climate](https://img.shields.io/codeclimate/github/codenautas/mini-tools.svg)](https://codeclimate.com/github/codenautas/mini-tools)
|
19 | [![dependencies](https://img.shields.io/david/codenautas/mini-tools.svg)](https://david-dm.org/codenautas/mini-tools)
|
20 |
|
21 |
|
22 |
|
23 | idioma: ![castellano](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-es.png)
|
24 | también disponible en:
|
25 | [![Inglés](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-en.png)](README.md)
|
26 |
|
27 |
|
28 | ## Instalación
|
29 |
|
30 | ## Install
|
31 | [!--lang:*-->
|
32 |
|
33 | ```sh
|
34 | $ npm install mini-tools
|
35 | ```
|
36 |
|
37 |
|
38 |
|
39 | ## Objetivo principal
|
40 |
|
41 | Tener a mano algunas herramientas pequeñas para express
|
42 |
|
43 |
|
44 |
|
45 | ## Main goal
|
46 |
|
47 | Have some mini tools for express and others
|
48 |
|
49 | [!--lang:*-->
|
50 |
|
51 | ## API
|
52 |
|
53 | ### serveErr(req, res [, next])
|
54 |
|
55 |
|
56 |
|
57 | Retorna una función que envía un mensaje de error al cliente
|
58 |
|
59 |
|
60 |
|
61 | Returns a function that sends a error message to de front-end.
|
62 |
|
63 | [!--lang:*-->
|
64 |
|
65 | ```js
|
66 | app.post('/insert' , function(req,res){
|
67 | //...
|
68 | if(duplicate){
|
69 | serveErr(req,res)(new Error("Duplicate name. Can't insert"));
|
70 | return;
|
71 | }
|
72 | //...
|
73 | ```
|
74 |
|
75 |
|
76 |
|
77 | Está diseñado para utilizarse con promesas
|
78 |
|
79 |
|
80 |
|
81 | It is promise friendly
|
82 |
|
83 | [!--lang:*-->
|
84 |
|
85 | ```js
|
86 | app.use('/tools', function(req,res,next){
|
87 | //...
|
88 | .then(function(){
|
89 | if(not_in_this_middleware){
|
90 | throw new Error("next");
|
91 | }
|
92 | // ...
|
93 | }).catch(serveErr(req,res,next));
|
94 | ```
|
95 |
|
96 |
|
97 |
|
98 | *catch* espera un función que recibe un error.
|
99 | *serveErr* devuelve esa función.
|
100 |
|
101 | Cuando err es Error("next") *serveErr* llama a next y no envía ningún mensaje al cliente
|
102 | (porque entiende que no es un error sino que debe capturarse en el siguiente middleware);
|
103 | de otro modo envía un error 400 con el mensaje de error (parámetro de Error).
|
104 |
|
105 |
|
106 |
|
107 | *catch* expects a function that receive an error.
|
108 | *serveErr* returns that function.
|
109 |
|
110 | When err is Error("next") *serveErr* calls next and does not send any result to de front-end;
|
111 | otherwise it sends a 400 error with the message and stack.
|
112 |
|
113 | [!--lang:*-->
|
114 |
|
115 | ### serveJade(path, any)
|
116 |
|
117 | ```js
|
118 | var express = require('express');
|
119 | var app = express();
|
120 |
|
121 | app.use('/',MiniTools.serveJade('./static',true));
|
122 |
|
123 | app.use('/main',MiniTools.serveJade('./static/index.jade',false));
|
124 | ```
|
125 |
|
126 |
|
127 |
|
128 | Retorna un middleware compatible con express que envía y compila archivos jade.
|
129 | Si `any==true` acepta cualquier nombre (sin extensión) y busca si existe el archivo .jade correspondiente;
|
130 | si no existe tal archivo llama a `next()` para que continúe con el siguiente middleware.
|
131 |
|
132 | Si `any==false` lo que se especifica es un archivo específico.
|
133 |
|
134 | **Nota**: hay que agregar `"jade"` a `package.json`
|
135 |
|
136 |
|
137 |
|
138 | Returns an express middleware to serve jade files.
|
139 | If `any==true` it serves files adding .jade to req.path; and
|
140 | if there is no jade file it call `next()`.
|
141 |
|
142 | If `any==false` it serves that specific file.
|
143 |
|
144 | **Note**: for use serveJade you must include jade in `package.json`
|
145 |
|
146 | [!--lang:*-->
|
147 |
|
148 | ### serveStylus(path, any)
|
149 |
|
150 | ```js
|
151 | var express = require('express');
|
152 | var app = express();
|
153 |
|
154 | app.use('/',MiniTools.serveStylus('./static',true));
|
155 |
|
156 | app.use('/site.css',MiniTools.serveStylus('./static/index.styl',false));
|
157 | ```
|
158 |
|
159 |
|
160 |
|
161 | Retorna un middleware compatible con express que envía y compila archivos jade.
|
162 | Si `any==true` acepta cualquier nombre (sin extensión) y busca si existe el archivo .jade correspondiente;
|
163 | si no existe tal archivo llama a `next()` para que continúe con el siguiente middleware.
|
164 |
|
165 | Si `any==false` lo que se especifica es un archivo específico.
|
166 |
|
167 | **Nota**: hay que agregar `"stylus"` a `package.json`
|
168 |
|
169 |
|
170 |
|
171 | Returns an express middleware to serve jade files.
|
172 | If `any==true` it serves files adding .jade to req.path; and
|
173 | if there is no jade file it call `next()`.
|
174 |
|
175 | If `any==false` it serves that specific file.
|
176 |
|
177 | **Note**: for use serveStylus you must include stylus in package.json
|
178 |
|
179 |
|
180 |
|
181 | ## Licencia
|
182 |
|
183 |
|
184 |
|
185 | ## License
|
186 |
|
187 | [!--lang:*-->
|
188 |
|
189 | [MIT](LICENSE)
|
190 |
|