UNPKG

12.4 kBMarkdownView Raw
1# Micro — Asynchronous HTTP microservices
2
3## Features
4
5- **Easy**: Designed for usage with `async` and `await`
6- **Fast**: Ultra-high performance (even JSON parsing is opt-in)
7- **Micro**: The whole project is ~260 lines of code
8- **Agile**: Super easy deployment and containerization
9- **Simple**: Oriented for single purpose modules (function)
10- **Standard**: Just HTTP!
11- **Explicit**: No middleware - modules declare all [dependencies](https://github.com/amio/awesome-micro)
12- **Lightweight**: With all dependencies, the package weighs less than a megabyte
13
14**Disclaimer:** Micro was created for use within containers and is not intended for use in serverless environments. For those using Vercel, this means that there is no requirement to use Micro in your projects as the benefits it provides are not applicable to the platform. Utility features provided by Micro, such as `json`, are readily available in the form of [Serverless Function helpers](https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-request-and-response-objects).
15
16## Installation
17
18**Important:** Micro is only meant to be used in production. In development, you should use [micro-dev](https://github.com/vercel/micro-dev), which provides you with a tool belt specifically tailored for developing microservices.
19
20To prepare your microservice for running in the production environment, firstly install `micro`:
21
22```bash
23npm install --save micro
24```
25
26## Usage
27
28Create an `index.js` file and export a function that accepts the standard [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage) and [http.ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse) objects:
29
30```js
31module.exports = (req, res) => {
32 res.end('Welcome to Micro');
33};
34```
35
36Micro provides [useful helpers](https://github.com/vercel/micro#body-parsing) but also handles return values – so you can write it even shorter!
37
38```js
39module.exports = () => 'Welcome to Micro';
40```
41
42Next, ensure that the `main` property inside `package.json` points to your microservice (which is inside `index.js` in this example case) and add a `start` script:
43
44```json
45{
46 "main": "index.js",
47 "scripts": {
48 "start": "micro"
49 }
50}
51```
52
53Once all of that is done, the server can be started like this:
54
55```bash
56npm start
57```
58
59And go to this URL: `http://localhost:3000` - 🎉
60
61### Command line
62
63```
64 micro - Asynchronous HTTP microservices
65
66 USAGE
67
68 $ micro --help
69 $ micro --version
70 $ micro [-l listen_uri [-l ...]] [entry_point.js]
71
72 By default micro will listen on 0.0.0.0:3000 and will look first
73 for the "main" property in package.json and subsequently for index.js
74 as the default entry_point.
75
76 Specifying a single --listen argument will overwrite the default, not supplement it.
77
78 OPTIONS
79
80 --help shows this help message
81
82 -v, --version displays the current version of micro
83
84 -l, --listen listen_uri specify a URI endpoint on which to listen (see below) -
85 more than one may be specified to listen in multiple places
86
87 ENDPOINTS
88
89 Listen endpoints (specified by the --listen or -l options above) instruct micro
90 to listen on one or more interfaces/ports, UNIX domain sockets, or Windows named pipes.
91
92 For TCP (traditional host/port) endpoints:
93
94 $ micro -l tcp://hostname:1234
95
96 For UNIX domain socket endpoints:
97
98 $ micro -l unix:/path/to/socket.sock
99
100 For Windows named pipe endpoints:
101
102 $ micro -l pipe:\\.\pipe\PipeName
103```
104
105### `async` & `await`
106
107<p><details>
108 <summary><b>Examples</b></summary>
109 <ul><li><a href="./examples/external-api-call">Fetch external api</a></li></ul>
110</details></p>
111
112Micro is built for usage with async/await.
113
114```js
115const sleep = require('then-sleep');
116
117module.exports = async (req, res) => {
118 await sleep(500);
119 return 'Ready!';
120};
121```
122
123### Port Based on Environment Variable
124
125When you want to set the port using an environment variable you can use:
126
127```
128micro -l tcp://0.0.0.0:$PORT
129```
130
131Optionally you can add a default if it suits your use case:
132
133```
134micro -l tcp://0.0.0.0:${PORT-3000}
135```
136
137`${PORT-3000}` will allow a fallback to port `3000` when `$PORT` is not defined.
138
139Note that this only works in Bash.
140
141### Body parsing
142
143<p id="body-parsing-examples"><details>
144 <summary><b>Examples</b></summary>
145 <ul>
146 <li><a href="./examples/json-body-parsing">Parse JSON</a></li>
147 <li><a href="./examples/urlencoded-body-parsing">Parse urlencoded form (html `form` tag)</a></li>
148 </ul>
149</details></p>
150
151For parsing the incoming request body we included an async functions `buffer`, `text` and `json`
152
153```js
154const { buffer, text, json } = require('micro');
155
156module.exports = async (req, res) => {
157 const buf = await buffer(req);
158 console.log(buf);
159 // <Buffer 7b 22 70 72 69 63 65 22 3a 20 39 2e 39 39 7d>
160 const txt = await text(req);
161 console.log(txt);
162 // '{"price": 9.99}'
163 const js = await json(req);
164 console.log(js.price);
165 // 9.99
166 return '';
167};
168```
169
170### API
171
172##### `buffer(req, { limit = '1mb', encoding = 'utf8' })`
173
174##### `text(req, { limit = '1mb', encoding = 'utf8' })`
175
176##### `json(req, { limit = '1mb', encoding = 'utf8' })`
177
178- Buffers and parses the incoming body and returns it.
179- Exposes an `async` function that can be run with `await`.
180- Can be called multiple times, as it caches the raw request body the first time.
181- `limit` is how much data is aggregated before parsing at max. Otherwise, an `Error` is thrown with `statusCode` set to `413` (see [Error Handling](#error-handling)). It can be a `Number` of bytes or [a string](https://www.npmjs.com/package/bytes) like `'1mb'`.
182- If JSON parsing fails, an `Error` is thrown with `statusCode` set to `400` (see [Error Handling](#error-handling))
183
184For other types of data check the [examples](#body-parsing-examples)
185
186### Sending a different status code
187
188So far we have used `return` to send data to the client. `return 'Hello World'` is the equivalent of `send(res, 200, 'Hello World')`.
189
190```js
191const { send } = require('micro');
192
193module.exports = async (req, res) => {
194 const statusCode = 400;
195 const data = { error: 'Custom error message' };
196
197 send(res, statusCode, data);
198};
199```
200
201##### `send(res, statusCode, data = null)`
202
203- Use `require('micro').send`.
204- `statusCode` is a `Number` with the HTTP status code, and must always be supplied.
205- If `data` is supplied it is sent in the response. Different input types are processed appropriately, and `Content-Type` and `Content-Length` are automatically set.
206 - `Stream`: `data` is piped as an `octet-stream`. Note: it is _your_ responsibility to handle the `error` event in this case (usually, simply logging the error and aborting the response is enough).
207 - `Buffer`: `data` is written as an `octet-stream`.
208 - `object`: `data` is serialized as JSON.
209 - `string`: `data` is written as-is.
210- If JSON serialization fails (for example, if a cyclical reference is found), a `400` error is thrown. See [Error Handling](#error-handling).
211
212### Programmatic use
213
214You can use Micro programmatically by requiring Micro directly:
215
216```js
217const http = require('http');
218const serve = require('micro');
219const sleep = require('then-sleep');
220
221const server = new http.Server(
222 serve(async (req, res) => {
223 await sleep(500);
224 return 'Hello world';
225 }),
226);
227
228server.listen(3000);
229```
230
231##### serve(fn)
232
233- Use `require('micro').serve`.
234- Returns a function with the `(req, res) => void` signature. That uses the provided `function` as the request handler.
235- The supplied function is run with `await`. So it can be `async`
236
237##### sendError(req, res, error)
238
239- Use `require('micro').sendError`.
240- Used as the default handler for errors thrown.
241- Automatically sets the status code of the response based on `error.statusCode`.
242- Sends the `error.message` as the body.
243- Stacks are printed out with `console.error` and during development (when `NODE_ENV` is set to `'development'`) also sent in responses.
244- Usually, you don't need to invoke this method yourself, as you can use the [built-in error handling](#error-handling) flow with `throw`.
245
246##### createError(code, msg, orig)
247
248- Use `require('micro').createError`.
249- Creates an error object with a `statusCode`.
250- Useful for easily throwing errors with HTTP status codes, which are interpreted by the [built-in error handling](#error-handling).
251- `orig` sets `error.originalError` which identifies the original error (if any).
252
253## Error Handling
254
255Micro allows you to write robust microservices. This is accomplished primarily by bringing sanity back to error handling and avoiding callback soup.
256
257If an error is thrown and not caught by you, the response will automatically be `500`. **Important:** Error stacks will be printed as `console.error` and during development mode (if the env variable `NODE_ENV` is `'development'`), they will also be included in the responses.
258
259If the `Error` object that's thrown contains a `statusCode` property, that's used as the HTTP code to be sent. Let's say you want to write a rate limiting module:
260
261```js
262const rateLimit = require('my-rate-limit');
263
264module.exports = async (req, res) => {
265 await rateLimit(req);
266 // ... your code
267};
268```
269
270If the API endpoint is abused, it can throw an error with `createError` like so:
271
272```js
273if (tooMany) {
274 throw createError(429, 'Rate limit exceeded');
275}
276```
277
278Alternatively you can create the `Error` object yourself
279
280```js
281if (tooMany) {
282 const err = new Error('Rate limit exceeded');
283 err.statusCode = 429;
284 throw err;
285}
286```
287
288The nice thing about this model is that the `statusCode` is merely a suggestion. The user can override it:
289
290```js
291try {
292 await rateLimit(req);
293} catch (err) {
294 if (429 == err.statusCode) {
295 // perhaps send 500 instead?
296 send(res, 500);
297 }
298}
299```
300
301If the error is based on another error that **Micro** caught, like a `JSON.parse` exception, then `originalError` will point to it. If a generic error is caught, the status will be set to `500`.
302
303In order to set up your own error handling mechanism, you can use composition in your handler:
304
305```js
306const { send } = require('micro');
307
308const handleErrors = (fn) => async (req, res) => {
309 try {
310 return await fn(req, res);
311 } catch (err) {
312 console.log(err.stack);
313 send(res, 500, 'My custom error!');
314 }
315};
316
317module.exports = handleErrors(async (req, res) => {
318 throw new Error('What happened here?');
319});
320```
321
322## Testing
323
324Micro makes tests compact and a pleasure to read and write.
325We recommend [Node TAP](https://node-tap.org/) or [AVA](https://github.com/avajs/ava), a highly parallel test framework with built-in support for async tests:
326
327```js
328const http = require('http');
329const { send, serve } = require('micro');
330const test = require('ava');
331const listen = require('test-listen');
332const fetch = require('node-fetch');
333
334test('my endpoint', async (t) => {
335 const service = new http.Server(
336 serve(async (req, res) => {
337 send(res, 200, {
338 test: 'woot',
339 });
340 }),
341 );
342
343 const url = await listen(service);
344 const response = await fetch(url);
345 const body = await response.json();
346
347 t.deepEqual(body.test, 'woot');
348 service.close();
349});
350```
351
352Look at [test-listen](https://github.com/vercel/test-listen) for a
353function that returns a URL with an ephemeral port every time it's called.
354
355## Contributing
356
3571. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device
3582. Link the package to the global module directory: `npm link`
3593. Within the module you want to test your local development instance of Micro, just link it to the dependencies: `npm link micro`. Instead of the default one from npm, node will now use your clone of Micro!
360
361You can run the tests using: `npm test`.
362
363## Credits
364
365Thanks to Tom Yandell and Richard Hodgson for donating the name "micro" on [npm](https://www.npmjs.com)!
366
367## Authors
368
369- Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [Vercel](https://vercel.com)
370- Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [Vercel](https://vercel.com)
371- Tim Neutkens ([@timneutkens](https://twitter.com/timneutkens)) - [Vercel](https://vercel.com)