例如使用koa编写一个web程序,为了测试效果,可能要频繁地去关闭、启动。
1、示例(index.js):
var koa = require('koa');var app = koa();var route = require('koa-route');var views = require('co-views');var static = require('koa-static');var router = require('koa-router')();var render = views(__dirname + '/views', { map: { html: 'swig' } });app.use(static(__dirname + '/static'));router.get('/', function *(next) { this.body = yield render('index', { });});router.get('/test', function *(next) { this.body = yield render('test', { pagename: 'awesome people', authors: ['Paul', 'Jim', 'Jane'] });});router.get('/hello', function *(next) { this.body = 'Hello, world!';});app.use(router.routes()).use(router.allowedMethods());app.listen(6680, '0.0.0.0', function(){ console.log('start listen http://0.0.0.0:6680');});
运行:
$ node index.js
2、解决方案
可以将gulp和nodemon结合起来。
$ npm install gulp-nodemon --save-dev
gulpfile.js:
var gulp = require('gulp'), nodemon = require('gulp-nodemon');gulp.task('default', function() { // 将你的默认的任务代码放在这});// 使用gulp start,当js、html后缀的文件代码改变时候会自动重启gulp.task('start', function () { nodemon({ script: 'index.js' , ext: 'js html css' , env: { 'NODE_ENV': 'development' } })})
此后,运行gulp start
即可。