Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back default http server wrapper #460

Merged
merged 2 commits into from Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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