UNPKG

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