function authtoken(token, cb) { cb = cb || noop; var a = spawn( bin, ['authtoken', token], {cwd: __dirname + '/bin'}); a.stdout.once('data', done.bind(null, null, token)); a.stderr.once('data', done.bind(null, new Error('cant set authtoken'))); function done(err, token) { cb(err, token); a.kill(); } }
...
ngrok http 8080
```
## authtoken
You can create basic http-https-tcp tunnel without authtoken. For custom subdomains and more you should obtain authtoken by signing
up at [ngrok.com](https://ngrok.com). Once you set it, it's stored in ngrok config and used for all tunnels. Few ways:
```
ngrok.authtoken(token, function(err, token) {});
ngrok.connect({authtoken: token, ...}, function (err, url) {});
ngrok authtoken <token>
```
## connect
```javascript
var ngrok = require('ngrok');
...
function connect(opts, cb) { if (typeof opts === 'function') { cb = opts; } cb = cb || noop; opts = defaults(opts); if (api) { return runTunnel(opts, cb); } lock('ngrok', function(release) { function run(err) { if (err) { emitter.emit('error', err); return cb(err); } runNgrok(opts, release(function(err) { if (err) { emitter.emit('error', err); return cb(err); } runTunnel(opts, cb) })); } opts.authtoken ? authtoken(opts.authtoken, run) : run(null); }); }
...
usage
===
[](https://nodei
.co/npm/ngrok/)
```
var ngrok = require('ngrok');
ngrok.connect(function (err, url) {});
or
npm install ngrok -g
ngrok http 8080
```
...
function disconnect(publicUrl, cb) { cb = cb || noop; if (typeof publicUrl === 'function') { cb = publicUrl; publicUrl = null; } if (!api) { return cb(); } if (publicUrl) { return api.del( tunnels[publicUrl], function(err, resp, body) { if (err || resp.statusCode !== 204) { return cb(err || new Error(body)); } delete tunnels[publicUrl]; emitter.emit('disconnect', publicUrl); return cb(); }); } return async.each( Object.keys(tunnels), disconnect, function(err) { if (err) { emitter.emit('error', err); return cb(err); } emitter.emit('disconnect'); return cb(); }); }
...
Other options: `name, inspect, host_header, bind_tls, hostname, crt, key, client_cas, remote_addr` - read [here](https://ngrok.com
/docs)
Note on regions: region used in first tunnel will be used for all next tunnels too.
## disconnect
The ngrok and all tunnels will be killed when node process is done. To stop the tunnels use
```javascript
ngrok.disconnect(url); // stops one
ngrok.disconnect(); // stops all
ngrok.kill(); // kills ngrok process
```
Note on http tunnels: by default bind_tls is true, so whenever you use http proto two tunnels are created - http and https. If you
disconnect https tunnel, http tunnel remains open. You might want to close them both by passing http-version url, or simply by
disconnecting all in one go ```ngrok.disconnect()```.
## emitter
...
function kill(cb) { cb = cb || noop; if (!ngrok) { return cb(); } ngrok.on('exit', function() { api = null; tunnels = {}; emitter.emit('disconnect'); return cb(); }); return ngrok.kill(); }
...
Note on regions: region used in first tunnel will be used for all next tunnels too.
## disconnect
The ngrok and all tunnels will be killed when node process is done. To stop the tunnels use
```javascript
ngrok.disconnect(url); // stops one
ngrok.disconnect(); // stops all
ngrok.kill(); // kills ngrok process
```
Note on http tunnels: by default bind_tls is true, so whenever you use http proto two tunnels are created - http and https. If you
disconnect https tunnel, http tunnel remains open. You might want to close them both by passing http-version url, or simply by
disconnecting all in one go ```ngrok.disconnect()```.
## emitter
Also you can use ngrok as an event emitter, it fires "connect", "disconnect" and "error" events
```javascript
...
error = function () {}
...
freebsdx64: cdn + cdnPath + 'freebsd-amd64.zip'
};
var arch = process.env.NGROK_ARCH || (os.platform() + os.arch());
var cdnFile = cdnFiles[arch];
if (!cdnFile) {
console.error('ngrok - platform ' + arch + ' is not supported.');
process.exit(1);
}
var localPath = __dirname + '/bin/';
var localFile = localPath + 'ngrok.zip';
console.log('ngrok - downloading binary ' + cdnFile + ' ...');
...