Skip to content

Instantly share code, notes, and snippets.

@spalladino
Created October 15, 2017 23:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spalladino/45a6e54d7942ac0bad64dd54d7d12467 to your computer and use it in GitHub Desktop.
Save spalladino/45a6e54d7942ac0bad64dd54d7d12467 to your computer and use it in GitHub Desktop.
Configure ngrok with nodemon for local development
#!/usr/bin/env node
if (process.env.NODE_ENV === 'production') {
throw new Error("Do not use nodemon in production, run bin/www.js directly instead");
}
const nodemon = require('nodemon');
const ngrok = require('ngrok');
// We start an ngrok tunnel to ensure it stays the same for the entire process
ngrok.connect({
proto: 'http',
addr: '3001'
}, (err, url) => {
if (err) {
console.error("Error opening ngrok tunnel", err);
process.exit(1);
} else {
// We inject the url as NGROK_URL env var into our node process,
// and have nodemon start our main web server process
clonsole.log("Ngrok tunnel opened at " + url);
nodemon(`-x 'NGROK_URL=${url} node' ./bin/www.js`);
}
});
nodemon.on('start', function () {
console.log('App has started');
}).on('quit', function () {
console.log('App has quit');
}).on('restart', function (files) {
console.log('App restarted due to: ', files);
});
@spalladino
Copy link
Author

This snippet starts an ngrok tunnel, and then runs nodemon with the main node process. This ensures that the generated ngrok URL stays the same during the entire development session, while nodemon reloads the app server as needed.

@ZarifS
Copy link

ZarifS commented Nov 17, 2019

Where do i place this in my express setup? right now my app.js is the where the server gets started and i am using nodemon script form package.json to watch the server

@spalladino
Copy link
Author

spalladino commented Nov 19, 2019

Where do i place this in my express setup? right now my app.js is the where the server gets started and i am using nodemon script form package.json to watch the server

@ZarifS it has been a while since I wrote this script, but you should place this at the root of your project, and ensure that app.js is listed as the main file of your package.json so nodemon picks it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment