UNPKG

4.98 kBMarkdownView Raw
1# mini-tools
2mini 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
16language: ![English](https://raw.githubusercontent.com/codenautas/multilang/master/img/lang-en.png)
17also 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
29Have some mini tools for express and others
30
31
32## API
33
34### serveErr(req, res [, next])
35
36
37Returns a function that sends a error message to de front-end.
38If 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
45app.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
55It is promise friendly
56
57
58```js
59app.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
73When err is Error("next") *serveErr* calls next and does not send any result to de front-end;
74otherwise it sends a 400 error with the message and stack.
75
76
77### serveJade(path, opts)
78
79```js
80var express = require('express');
81var app = express();
82
83app.use('/',MiniTools.serveJade('./static',true));
84
85app.use('/main',MiniTools.serveJade('./static/index.jade',false));
86```
87
88
89Returns an express middleware to serve jade files.
90
91If *opts* is boolean it will be the *any* option.
92
93If `any==true` it serves files adding .jade to req.path; and
94if there is no jade file it call `next()`.
95
96If `any==false` it serves that specific file.
97
98Others 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
106var express = require('express');
107var app = express();
108
109app.use('/',MiniTools.serveStylus('./static',true));
110
111app.use('/site.css',MiniTools.serveStylus('./static/index.styl',false));
112```
113
114
115Returns an express middleware to serve jade files.
116If `any==true` it serves files adding .jade to req.path; and
117if there is no jade file it call `next()`.
118
119If `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
127var express = require('express');
128var app = express();
129
130app.use('/about',MiniTools.serveText('<h1>This app</h1>','html'));
131
132app.use('/is-up-service',MiniTools.serveText('Yes.'));
133```
134
135
136Returns an express middleware to serve pain text.
137Optionaly you can pass "content type".
138
139
140### serveJson(object)
141
142```js
143var express = require('express');
144var app = express();
145
146var config = {devel:false, title: "title"};
147
148app.use('/config',MiniTools.serveJson(config));
149```
150
151
152Returns an express middleware to serve an object in JSON format.
153
154
155### serveYaml(object)
156
157```js
158var express = require('express');
159var app = express();
160
161var config = {devel:false, title: "title"};
162
163app.use('/config',MiniTools.serveYaml(config));
164```
165
166
167Returns 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
174MiniTools.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
188Reads the chain of configuration merging with [best-globals.changing](https://www.npmjs.com/package/best-globals#changingoriginalconfig-changes-options).
189
190If the list element is a fileName ending with .json .yaml o .yml, it reads and parse,
191if doesn't have extension it search first,
192if 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