Created
December 24, 2017 17:25
-
-
Save arleighdickerson/c16cf00a2dd96b39c2c3a5fb2fb211f3 to your computer and use it in GitHub Desktop.
hot reloading of server-side feathersjs application (put this in src/)
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
const logger = require('winston'); | |
const invalidate = require('invalidate-module'); | |
const { resolve } = require('path'); | |
const chokidar = require('chokidar'); | |
process.on('unhandledRejection', (reason, p) => { | |
logger.error('Unhandled Rejection at: Promise ', p, reason); | |
}); | |
const src = (...args) => resolve(__dirname, ...args); | |
const getApplication = (() => { | |
let app = null; | |
chokidar | |
.watch([src()], { | |
ignoreInitial: true, | |
awaitWriteFinish: { | |
stabilityThreshold: 200, | |
pollInterval: 50, | |
}, | |
}) | |
.unwatch(src('index.js')) | |
.on('all', (evt, filename) => { | |
if (filename.endsWith('.js')) { | |
logger.info(`reloaded ${filename}`); | |
invalidate(resolve(filename)); | |
app = null; | |
} | |
}); | |
return () => { | |
if (app === null) { | |
app = require('./app'); | |
} | |
return app; | |
}; | |
})(); | |
const handler = { | |
get(target, name) { | |
return target()[name]; | |
}, | |
set(target, name, value) { | |
target()[name] = value; | |
}, | |
apply(target, that, args) { | |
return target().apply(that, args); | |
}, | |
}; | |
const proxy = new Proxy(getApplication, handler); | |
const hostname = getApplication().get('host'); | |
const port = getApplication().get('port'); | |
const server = proxy.listen(port); | |
server.on('listening', () => { | |
logger.info('Feathers application started on http://%s:%d', hostname, port); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment