vercel / micro

Asynchronous HTTP microservices
MIT License
10.59k stars 458 forks source link

Please consider updating the micro README #445

Closed nkhil closed 3 years ago

nkhil commented 3 years ago

While trying to understand a legacy codebase, I came across micro.

While trying to grok micro, I tried using the following example:

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

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

server.listen(3000)

as well as


const http = require('http')
const micro = require('micro')

const server = new http.Server(micro(async (req, res) => {
  micro.send(res, 200, 'hello world?')
}))

server.listen(3000)

However, trying to cURL the http://localhost:3000 endpoint via my terminal (and postman), my request hangs.

I'm probably doing something wrong, but I don't have any feedback or error messages to indicate how I can get micro to work.

If I get micro to work, I'll create a PR to update the quick start examples in the readme.

cezary commented 3 years ago

I ran into this as well, found this older issue. I think the readme you're using doesn't match the version you have installed. If you look at https://www.npmjs.com/package/micro, calling micro should return a server instance that you can call listen on:

const micro = require('micro')
const sleep = require('then-sleep')

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

server.listen(3000)