我已经在这里阅读了很多关于chokidar的问题和答案,但是我仍然感到难过...所以非常感谢任何可以调试我特定代码段的人。
我在localhost:3000运行基本的Express节点应用程序。
入口点是app.js:
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const clearRequire = require('clear-require');
const production = process.env.NODE_ENV === 'production';
const app = express();
// config nunjucks
const nunjucks = require('nunjucks');
nunjucks.configure('views', {
autoescape: true,
watch: true,
nocache: true,
express: app
});
// Routes for Express into modules
var index = require('./routes/index');
var game = require('./routes/game');
if (!production) {
const chokidar = require('chokidar');
const watcher = chokidar.watch('./routes');
watcher
.on('ready', () => console.log('Scan complete. Ready for changes.'))
.on('change', path => {
var clearPath = "./" + path.split(".")[0];
clearRequire(clearPath);
});
}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'njk');
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/game', game);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error', { test: 'test'});
next();
});
module.exports = app;
并在./routes/game.js内:
var express = require('express');
var router = express.Router();
/* GET game page. */
router.get('/', function(req, res) {
res.render('game', { title: 'Game', also: 'test-4' });
});
module.exports = router;
更新game.js(例如,更改" test-4"到" test-5")对应用程序没有影响而不重新启动服务器。我曾经假设chokidar会允许我更新game.js并让更改反映在下一页加载?
作为参考,game.njk模板如下所示:
{% extends "app.njk" %}
{% block pageContent %}
<div id="pageContent" class="container">
This. is. game. {{ also }}
</div><!-- /#pageContent .container -->
{% endblock pageContent %}
如果我更新模板,它会立即更新(根据nunjucks watch:true statement)但是对var var更新也没有运气。
感谢任何帮助。
答案 0 :(得分:2)
在清除缓存时,您不会续订game
路由。缓存刷新将适用于新所需的模块,但不会对已经需要的模块产生任何影响。因此,您需要在清除缓存后再次要求游戏路线。
watcher
.on('ready', () => console.log('Scan complete. Ready for changes.'))
.on('change', path => {
var clearPath = "./" + path.split(".")[0];
clearRequire(clearPath);
//Move this code elsewhere or not...
if(path == "routes/game.js"){
//renew game route
game = require("./routes/game");
}
});
然后改变:
app.use('/game', game); //Changing game won't have any effect
要:
app.use('/game', function(req, res, next){
game(req, res, next); //This game will have the renewed route
});
无论如何,我建议使用nodemon
Nodemon是一个实用程序,它将监视源中的任何更改并自动重新启动服务器。完美的发展。使用npm安装它。
只需使用nodemon而不是node来运行您的代码,现在就是您的代码 代码更改时,进程将自动重新启动。安装, 获取node.js,然后从终端运行:
npm install -g nodemon
然后:
nodemon server.js