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