我想改善我的節點的DEV體驗。爲此,我想:
a)當服務器端代碼改變時重啓我的服務器
b)當客戶端代碼改變時刷新瀏覽器。
爲了實現這個目標,我開始將nodemon和browserSync集成到我的gulp腳本中。Node.js - 自動刷新在發展
在我一飲而盡劇本,我有以下任務:
gulp.task('startDevEnv', function(done) {
// Begin watching for server-side file changes
nodemon(
{ script: input.server, ignore:[input.views] })
.on('start', function() {
browserSync.init({
proxy: "http://localhost:3002"
});
})
;
// Begin watching client-side file changes
gulp.watch([ input.css, input.js, input.html ], function() { browserSync.reload(); });
done();
});
當上述任務運行時,我的瀏覽器打開http://localhost:3000/。按預期顯示我的應用程序。但是,在控制檯窗口中,我注意到:
Error: listen EADDRINUSE :::3002
我理解到一定程度。我在我的server.js文件中有app.set('port', process.env.PORT || 3002);
。然而,我認爲這是設定代理價值的目的。不過,每當我修改代碼,我看到在我的控制檯窗口下面的相關錯誤:
[07:08:19] [nodemon] restarting due to changes...
[07:08:19] [nodemon] starting `node ./dist/server.js`
events.js:142
throw er; // Unhandled 'error' event
^
TypeError: args.cb is not a function
at Object.init (/Users/me/Website/Develop/node_modules/browser-sync/lib/public/init.js:25:25)
at null.<anonymous> (/Users/me/Website/Develop/gulpfile.js:142:25)
at emitNone (events.js:73:20)
at emit (events.js:167:7)
at Object.run (/Users/me/Website/Develop/node_modules/nodemon/lib/monitor/run.js:97:7)
at Function.run.kill (/Users/me/Website/Develop/node_modules/nodemon/lib/monitor/run.js:221:7)
at null.<anonymous> (/Users/me/Website/Develop/node_modules/nodemon/lib/monitor/run.js:333:7)
at emitOne (events.js:83:20)
at emit (events.js:170:7)
at restartBus (/Users/me/Website/Develop/node_modules/nodemon/lib/monitor/watch.js:162:7)
Me-MBP:Develop me$ events.js:142
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3002
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at Server._listen2 (net.js:1238:14)
at listen (net.js:1274:10)
at Server.listen (net.js:1370:5)
at Object.<anonymous> (/Users/me/Website/Develop/dist/server.js:70:8)
at Module._compile (module.js:399:26)
at Object.Module._extensions..js (module.js:406:10)
at Module.load (module.js:345:32)
at Function.Module._load (module.js:302:12)
在這一點上,我的代碼更改不會出現在我的瀏覽器。我不明白我做錯了什麼。我懷疑我的端口配置錯誤。但是,我不確定他們應該如何設置。
默認情況下BrowserSync使用端口3000. BrowserSync還使用端口3001作爲BrowserSync UI。出於這兩個原因,我想我會在我的server.js文件中將端口設置爲3002,並創建如上所示的代理。我究竟做錯了什麼?
'nodemon' http://stackoverflow.com/questions/24750395/reload-express-js-routes-changes-without-重啓服務器 –
你問過類似這裏http://stackoverflow.com/questions/35576663/using-browser-sync-with-node-js-app/35740475#35740475 – Satyajeet
這是一個ExpressJS應用程序? – Trevor