Skip to content

Commit

Permalink
Bring back default http server wrapper (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
walaszczykm committed Jul 31, 2022
1 parent e69ce5b commit 6b973cb
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
17 changes: 7 additions & 10 deletions packages/micro/README.md
Expand Up @@ -214,18 +214,15 @@ 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 micro = require('micro')
const sleep = require('then-sleep')

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

server.listen(3000);
server.listen(3000)
```

##### micro(fn)
Expand Down
3 changes: 2 additions & 1 deletion packages/micro/lib/index.js
@@ -1,4 +1,5 @@
// Native
const http = require('http');
const {Stream} = require('stream');

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

const serve = fn => (req, res) => exports.run(req, res, fn);
const serve = fn => new http.Server((req, res) => exports.run(req, res, fn));

module.exports = serve;
exports = serve;
Expand Down
4 changes: 2 additions & 2 deletions test/_test-utils.js
@@ -1,3 +1,3 @@
module.exports = ({http, micro, listen}) => ({
getUrl: fn => listen(new http.Server(micro(fn)))
module.exports = ({ micro, listen }) => ({
getUrl: (fn) => listen(micro(fn)),
});
3 changes: 1 addition & 2 deletions test/development.js
Expand Up @@ -2,12 +2,11 @@
const test = require('ava');
const fetch = require('node-fetch');
const listen = require('test-listen');
const http = require('http');

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

const {getUrl} = require('./_test-utils')({http, micro, listen});
const {getUrl} = require('./_test-utils')({micro, listen});

test('send(200, <Object>) is pretty-printed', async t => {
const fn = () => ({woot: 'yes'});
Expand Down
3 changes: 1 addition & 2 deletions test/index.js
@@ -1,12 +1,11 @@
// Packages
const http = require('http');
const test = require('ava');
const fetch = require('node-fetch');
const sleep = require('then-sleep');
const resumer = require('resumer');
const listen = require('test-listen');
const micro = require('../packages/micro/lib');
const {getUrl} = require('./_test-utils')({http, micro, listen});
const {getUrl} = require('./_test-utils')({micro, listen});

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

Expand Down
3 changes: 1 addition & 2 deletions test/production.js
@@ -1,13 +1,12 @@
// Packages
const http = require('http');
const test = require('ava');
const fetch = require('node-fetch');
const listen = require('test-listen');

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

const {getUrl} = require('./_test-utils')({http, micro, listen});
const {getUrl} = require('./_test-utils')({micro, listen});

test.serial('errors are printed in console in production', async t => {
let logged = false;
Expand Down

0 comments on commit 6b973cb

Please sign in to comment.