ngrok / ngrok-javascript

Embed ngrok secure ingress into your Node.js apps with a single line of code.
https://ngrok.com
Apache License 2.0
94 stars 18 forks source link

Add examples for eight more frameworks #36

Closed bobzilladev closed 1 year ago

bobzilladev commented 1 year ago

Almost all the files in this PR are auto-generated by the various frameworks, but are necessary to test the actual example code. The following files linked to in the README.md are the new ngrok pieces:

bobzilladev commented 1 year ago

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

nijikokun commented 1 year ago

Few pieces of feedback:

Framework Specific:

Example of listening then attaching ngrok with async/await:

// Listen to server
server.listen(port, async function () {
  // Start ngrok process
  if (process.env.NGROK_TOKEN) {
    await ngrok.authtoken(process.env.NGROK_TOKEN);
    url = await ngrok.connect(port);
  }
  console.log(`Server is listening on ${port}!`)
  console.log(`Server available at ${url}`)
})
bobzilladev commented 1 year ago

@nijikokun Thanks for all the feedback! I've pushed a commit that hopefully addresses everything.

Each example should be self-contained project in it's own folder similar to Prisma and NextJS examples.

Migrated to this layout.

Point users to use degit for cloning and running an example: npx degit github:user/repo/directory/sub-directory#branch-name then npm i

Added info to Examples section of README.md.

Express: What local port is this running on?

Added logging of socket address.

Express: How do I run multiple express servers with this?

Can call listen(app) with multiple app's and each will forward on a separate generated pipe/tcp socket.

Express: How does this work with something like nodemon?

Looks like nodemon restarts the entire node process, so would want to use tunnel with domain to have a deterministic domain to iterate on.

Start the server listener then point ngrok.

Have reordered the setup calls in Fastify, Hapi, and Nest.

Fastify: I would also expect to use async/await.

Moved ngrok setup into async block to use await rather than promise methods.

Koa: require...would move to the top of the file

Moved.

Next: Make tunnel is weird, should probably be based on ENV variable.

Unfortunately Next first runs a child process to build, then the parent does its setup, so an environment variable can't maintain a single tunnel. I've added a comment around this non-intuitive behavior.

Next: Port and host...as env variables.

Caught PORT in Remix but missed in Next, have added. There doesn't appear to be Next support for host via env.

Would suggest putting ngrok logic into it's own file and requiring it inside the config file to keep things clean.

Moved logic to ngrok.config files in Next.js, Remix, Svelt, and Vue examples and updated README.

nijikokun commented 1 year ago

The changes look great! Nice work!

Just looks like there are some leftover files in the examples directory (node modules and package.json)?

bobzilladev commented 1 year ago

I've removed the node_modules symlink. Kept the package.json for the basic single-file examples which need ngrok, ts-node, and winston.