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 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); | |
}); |
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
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
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.