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 | [![qa-control](http://codenautas.com/github/codenautas/mini-tools.svg)](http://codenautas.com/github/codenautas/mini-tools)
|
14 |
|
15 |
|
16 | language: ![English](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-en.png)
|
17 | also available in:
|
18 | [![Spanish](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-es.png)](LEEME.md)
|
19 |
|
20 | ## Install
|
21 |
|
22 | ```sh
|
23 | $ npm install mini-tools
|
24 | ```
|
25 |
|
26 |
|
27 | ## Main goal
|
28 |
|
29 | Have some mini tools for express and others
|
30 |
|
31 |
|
32 | ## API
|
33 |
|
34 | ### serveErr(req, res [, next])
|
35 |
|
36 |
|
37 | Returns a function that sends a error message to de front-end.
|
38 | If the error object has setted the property
|
39 |
|
40 | * code: is displayed before the message
|
41 | * status: is sended in the header (otherwise "400" is sended)
|
42 |
|
43 |
|
44 | ```js
|
45 | app.post('/insert' , function(req,res){
|
46 | //...
|
47 | if(duplicate){
|
48 | serveErr(req,res)(new Error("Duplicate name. Can't insert"));
|
49 | return;
|
50 | }
|
51 | //...
|
52 | ```
|
53 |
|
54 |
|
55 | It is promise friendly
|
56 |
|
57 |
|
58 | ```js
|
59 | app.use('/tools', function(req,res,next){
|
60 | //...
|
61 | .then(function(){
|
62 | if(not_in_this_middleware){
|
63 | throw new Error("next");
|
64 | }
|
65 | // ...
|
66 | }).catch(serveErr(req,res,next));
|
67 | ```
|
68 |
|
69 |
|
70 | *catch* expects a function that receive an error.
|
71 | *serveErr* returns that function.
|
72 |
|
73 | When err is Error("next") *serveErr* calls next and does not send any result to de front-end;
|
74 | otherwise it sends a 400 error with the message and stack.
|
75 |
|
76 |
|
77 | ### serveJade(path, opts)
|
78 |
|
79 | ```js
|
80 | var express = require('express');
|
81 | var app = express();
|
82 |
|
83 | app.use('/',MiniTools.serveJade('./static',true));
|
84 |
|
85 | app.use('/main',MiniTools.serveJade('./static/index.jade',false));
|
86 | ```
|
87 |
|
88 |
|
89 | Returns an express middleware to serve jade files.
|
90 |
|
91 | If *opts* is boolean it will be the *any* option.
|
92 |
|
93 | If `any==true` it serves files adding .jade to req.path; and
|
94 | if there is no jade file it call `next()`.
|
95 |
|
96 | If `any==false` it serves that specific file.
|
97 |
|
98 | Others options in *opts* are pased to `pug.render` function.
|
99 |
|
100 | **Note**: for use serveJade you must include `"pug"` in `package.json`
|
101 |
|
102 |
|
103 | ### serveStylus(path, any)
|
104 |
|
105 | ```js
|
106 | var express = require('express');
|
107 | var app = express();
|
108 |
|
109 | app.use('/',MiniTools.serveStylus('./static',true));
|
110 |
|
111 | app.use('/site.css',MiniTools.serveStylus('./static/index.styl',false));
|
112 | ```
|
113 |
|
114 |
|
115 | Returns an express middleware to serve jade files.
|
116 | If `any==true` it serves files adding .jade to req.path; and
|
117 | if there is no jade file it call `next()`.
|
118 |
|
119 | If `any==false` it serves that specific file.
|
120 |
|
121 | **Note**: for use serveStylus you must include stylus in package.json
|
122 |
|
123 |
|
124 | ### serveText(anyText,contentTypeText)
|
125 |
|
126 | ```js
|
127 | var express = require('express');
|
128 | var app = express();
|
129 |
|
130 | app.use('/about',MiniTools.serveText('<h1>This app</h1>','html'));
|
131 |
|
132 | app.use('/is-up-service',MiniTools.serveText('Yes.'));
|
133 | ```
|
134 |
|
135 |
|
136 | Returns an express middleware to serve pain text.
|
137 | Optionaly you can pass "content type".
|
138 |
|
139 |
|
140 | ### serveJson(object)
|
141 |
|
142 | ```js
|
143 | var express = require('express');
|
144 | var app = express();
|
145 |
|
146 | var config = {devel:false, title: "title"};
|
147 |
|
148 | app.use('/config',MiniTools.serveJson(config));
|
149 | ```
|
150 |
|
151 |
|
152 | Returns an express middleware to serve an object in JSON format.
|
153 |
|
154 |
|
155 | ### serveYaml(object)
|
156 |
|
157 | ```js
|
158 | var express = require('express');
|
159 | var app = express();
|
160 |
|
161 | var config = {devel:false, title: "title"};
|
162 |
|
163 | app.use('/config',MiniTools.serveYaml(config));
|
164 | ```
|
165 |
|
166 |
|
167 | Returns an express middleware to serve an object in yaml format
|
168 | (using [js-yaml](https:www.npmjs.com/package/js-yaml)).
|
169 |
|
170 |
|
171 | ### readConfig(list, opts)
|
172 |
|
173 | ```js
|
174 | MiniTools.readConfig(
|
175 | [
|
176 | {production: true},
|
177 | 'package.json',
|
178 | 'other-configs.yml',
|
179 | 'more-configs',
|
180 | ],
|
181 | {whenNotExist:'ignore'}
|
182 | ).then(function(config){
|
183 | console.log(config);
|
184 | });
|
185 | ```
|
186 |
|
187 |
|
188 | Reads the chain of configuration merging with [best-globals.changing](https://www.npmjs.com/package/best-globals#changingoriginalconfig-changes-options).
|
189 |
|
190 | If the list element is a fileName ending with .json .yaml o .yml, it reads and parse,
|
191 | if doesn't have extension it search first,
|
192 | if it is a plain object it uses directly.
|
193 |
|
194 | **options**
|
195 | * whenNotExist:'ignore'
|
196 | * whenNotExist:'fail'
|
197 |
|
198 |
|
199 | ## License
|
200 |
|
201 |
|
202 | [MIT](LICENSE)
|
203 |
|