Skip to content

Commit

Permalink
Add new programmatic usage (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed May 11, 2019
1 parent 726aec8 commit df157df
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
12 changes: 7 additions & 5 deletions README.md
Expand Up @@ -244,13 +244,14 @@ module.exports = async (req, res) => {
You can use Micro programmatically by requiring Micro directly:

```js
const http = require('http')
const micro = require('micro')
const sleep = require('then-sleep')

const server = micro(async (req, res) => {
const server = new http.Server(micro(async (req, res) => {
await sleep(500)
return 'Hello world'
})
}))

server.listen(3000)
```
Expand All @@ -259,7 +260,7 @@ server.listen(3000)

- This function is exposed as the `default` export.
- Use `require('micro')`.
- Returns a [`http.Server`](https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_class_http_server) that uses the provided `function` as the request handler.
- Returns a function with the `(req, res) => void` signature. That uses the provided `function` as the request handler.
- The supplied function is run with `await`. So it can be `async`

##### sendError(req, res, error)
Expand Down Expand Up @@ -353,17 +354,18 @@ Micro makes tests compact and a pleasure to read and write.
We recommend [ava](https://github.com/sindresorhus/ava), a highly parallel Micro test framework with built-in support for async tests:

```js
const http = require('http')
const micro = require('micro')
const test = require('ava')
const listen = require('test-listen')
const request = require('request-promise')

test('my endpoint', async t => {
const service = micro(async (req, res) => {
const service = new http.Server(micro(async (req, res) => {
micro.send(res, 200, {
test: 'woot'
})
})
}))

const url = await listen(service)
const body = await request(url)
Expand Down
3 changes: 1 addition & 2 deletions lib/index.js
@@ -1,5 +1,4 @@
// Native
const server = require('http').Server;
const {Stream} = require('stream');

// Packages
Expand All @@ -23,7 +22,7 @@ function readable(stream) {
const {NODE_ENV} = process.env;
const DEV = NODE_ENV === 'development';

const serve = fn => server((req, res) => exports.run(req, res, fn));
const serve = fn => (req, res) => exports.run(req, res, fn);

module.exports = serve;
exports = serve;
Expand Down
3 changes: 2 additions & 1 deletion test/development.js
Expand Up @@ -2,12 +2,13 @@
const test = require('ava');
const request = require('request-promise');
const listen = require('test-listen');
const http = require('http');

process.env.NODE_ENV = 'development';
const micro = require('../');

const getUrl = fn => {
const srv = micro(fn);
const srv = new http.Server(micro(fn));

return listen(srv);
};
Expand Down
7 changes: 6 additions & 1 deletion test/index.js
@@ -1,4 +1,5 @@
// Packages
const http = require('http');
const test = require('ava');
const request = require('request-promise');
const sleep = require('then-sleep');
Expand All @@ -8,7 +9,11 @@ const micro = require('../');

const {send, sendError, buffer, json} = micro;

const getUrl = fn => listen(micro(fn));
const getUrl = fn => {
const srv = new http.Server(micro(fn));

return listen(srv);
};

test('send(200, <String>)', async t => {
const fn = async (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion test/production.js
@@ -1,4 +1,5 @@
// Packages
const http = require('http');
const test = require('ava');
const request = require('request-promise');
const listen = require('test-listen');
Expand All @@ -7,7 +8,7 @@ process.env.NODE_ENV = 'production';
const micro = require('../');

const getUrl = fn => {
const srv = micro(fn);
const srv = new http.Server(micro(fn));

return listen(srv);
};
Expand Down

0 comments on commit df157df

Please sign in to comment.