From f4017d64ef5c1d793aad7c6a80777e2155447383 Mon Sep 17 00:00:00 2001 From: Ali Sabzevari Date: Mon, 28 Jan 2019 15:20:02 +0100 Subject: [PATCH] #369 update examples --- examples/socket.io-chat-app/index.js | 2 +- examples/with-graphql-request/README.md | 3 +-- examples/with-https/README.md | 2 +- examples/with-https/index.js | 11 ++++++----- src/index.ts | 21 ++++++++++++++++++--- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/socket.io-chat-app/index.js b/examples/socket.io-chat-app/index.js index 8b8d2a9b..864b7f9d 100644 --- a/examples/socket.io-chat-app/index.js +++ b/examples/socket.io-chat-app/index.js @@ -1,4 +1,4 @@ -const { micro } = require('micro'); +const micro = require('micro'); const fs = require('fs'); const path = require('path'); diff --git a/examples/with-graphql-request/README.md b/examples/with-graphql-request/README.md index 89885478..2940092a 100644 --- a/examples/with-graphql-request/README.md +++ b/examples/with-graphql-request/README.md @@ -18,8 +18,7 @@ $ yarn run start # (or `$ npm run start`) Test it: ```bash -curl --request GET \ - --url http://localhost:3000/ +curl http://localhost:3000/ # Expected curl result: # {"releaseDate":"2010-08-28T20:00:00.000Z","actors":[{"name":"Leonardo DiCaprio"},{"name":"Ellen Page"},{"name":"Tom Hardy"},{"name":"Joseph Gordon-Levitt"},{"name":"Marion Cotillard"}]} diff --git a/examples/with-https/README.md b/examples/with-https/README.md index cf0c25a2..a2ee1b9c 100644 --- a/examples/with-https/README.md +++ b/examples/with-https/README.md @@ -18,7 +18,7 @@ npm run start Test it: ```bash -curl -k --request GET --url https://localhost:3443/ +curl -k https://localhost:3443/ # Expected curl result: # {"encrypted":true} diff --git a/examples/with-https/index.js b/examples/with-https/index.js index ab58cf10..0066096c 100644 --- a/examples/with-https/index.js +++ b/examples/with-https/index.js @@ -1,5 +1,5 @@ const https = require('https'); -const { listener, res } = require('micro'); +const { run, send } = require('micro'); const { key, cert, passphrase } = require('openssl-self-signed-certificate'); @@ -7,11 +7,12 @@ const PORT = process.env.PORT || 3443; const options = { key, cert, passphrase }; -const microHttps = fn => https.createServer(options, listener(fn)); +const microHttps = fn => + https.createServer(options, (req, res) => run(req, res, fn)); -const handler = req => res({ encrypted: req.client.encrypted }, 200); - -const server = microHttps(handler); +const server = microHttps(async (req, res) => { + send(res, 200, { encrypted: req.client.encrypted }); +}); server.listen(PORT); console.log(`Listening on https://localhost:${PORT}`); diff --git a/src/index.ts b/src/index.ts index 822edc8f..c2129c79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,11 +2,9 @@ import { Server } from "http"; import { HttpHandler, run, send, serve, sendError } from "./micro"; import { buffer, json, text } from "./helpers"; -export { res, Body, HttpRequest, HttpResponse } from "./http-message"; +import { res, Body, HttpRequest, HttpResponse } from "./http-message"; import { createError } from "./error"; -export { HttpHandler }; - export interface Micro { (fn: HttpHandler): Server; send: typeof send; @@ -28,3 +26,20 @@ micro.text = text; micro.json = json; export default micro as Micro; +export { + HttpHandler, + run, + send, + serve, + sendError, + buffer, + json, + text, + res, + Body, + HttpRequest, + HttpResponse, + createError +} +module.exports = serve; +exports = serve;