Skip to content

Instantly share code, notes, and snippets.

@zwhitchcox
Created April 21, 2016 20:30
Show Gist options
  • Save zwhitchcox/5fcb45c5483b95f47949bdeaf7b75c64 to your computer and use it in GitHub Desktop.
Save zwhitchcox/5fcb45c5483b95f47949bdeaf7b75c64 to your computer and use it in GitHub Desktop.
Start a server and restart it any time there is a change in a file
'use strict'
const path = require('path')
const cp = require('child_process')
const gaze = require('gaze')
// edit to match the path of your server
const serverPath = __dirname+'/index'
let server;
let serverScript = 'require(\''+serverPath+'\')'
function runServer(cb) {
if (server) {
server.kill('SIGTERM')
}
server = cp.spawn('node', ['--eval', serverScript], {
env: Object.assign({ NODE_ENV: 'dev' }, process.env),
stdio: 'inherit',
silent: false,
})
}
process.on('exit', () => {
if (server) {
server.kill('SIGTERM')
}
})
runServer()
gaze(['**','!node_modules/**'], function(err, watcher) {
this.on('all', function(event, filepath) {
runServer()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment