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

Allow using micro-dev programmatically #89

Merged
merged 1 commit into from
Dec 4, 2018
Merged

Allow using micro-dev programmatically #89

merged 1 commit into from
Dec 4, 2018

Conversation

elnygren
Copy link

@elnygren elnygren commented Aug 16, 2018

Hi! I wanted to submit an idea I'm using myself with ts-node-dev in a TypeScript + (Apollo) GraphQL project where I use micro as my server library.

Problem

Using a package.json script such as the following:

"start": "ts-node-dev node_modules/.bin/micro-dev -p 4000 src/server.ts",

causes the process to crash on syntax errors etc. With micro this can be fixed by programmatically using micro:

# package.json
"start": "ts-node-dev src/server.ts"

# server.ts
const server = require('micro')(handler)
server.listen(PORT)

However, as we all know (vercel/micro#337) micro-dev does not support programmatical usage. While many of the features are CLI specific, some are not.

Solution

With this PR in place, one can do something like:

const PORT = process.env.PORT || 4000

if (process.env.NODE_ENV === 'development') {
  const microDev = require('micro-dev')
  microDev({ silent: false, limit: '1mb', host: '::', port: PORT })(handler)
} else {
  const server = require('micro')(handler)
  server.listen(PORT)
}

Comments

  • you guys might prefer Use micro & micro-dev programmatically? micro#337 (comment)
  • the message part breaks DRY but we can clean that up if this direction feels correct
  • we're not really supporting too many micro-dev features
  • it might make sense to not call .listen within micro-dev when using the library like this. It would make this PR simpler and the usage a bit cleaner:
const server = require('micro')(handler)
if (process.env.NODE_ENV === 'development') {
  server = require('micro-dev')({ silent: false, limit: '1mb'})(handler)
}
server.listen(PORT)

It nothing else, this might act as a good reference for other micro & TypeScript users!

Cheers :)

@jamo jamo requested a review from leo August 17, 2018 07:07
@leo leo removed their request for review December 3, 2018 10:08
@rauchg rauchg requested a review from leo December 3, 2018 20:05
Copy link
Contributor

@leo leo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, thank you!

@leo leo changed the title allow using micro-dev programmatically Allow using micro-dev programmatically Dec 4, 2018
@leo leo merged commit bda3bab into vercel:master Dec 4, 2018
@qndrey
Copy link

qndrey commented Feb 1, 2019

Would be nice to release this change. Really appreciated.

@brandonpsmith
Copy link

Please release this change. Much appreciated!

@frattaro
Copy link

frattaro commented Jul 12, 2019

Since this isn't published yet, someone should add another PR to return the server object from the index module, so that you can programmatically call:

  server.close(function () {
    console.log('Server shutdown.')
    process.exit()
  })

@ghost
Copy link

ghost commented Jul 27, 2019

Could we get a release with this? Now that next includes an API system - it would be really nice to include dev output via the next runtime.

@nathanredblur
Copy link

please release this change :) thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants