Created
October 15, 2017 23:15
-
-
Save spalladino/45a6e54d7942ac0bad64dd54d7d12467 to your computer and use it in GitHub Desktop.
Configure ngrok with nodemon for local development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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 themain
file of yourpackage.json
so nodemon picks it up.