gpt4 book ai didi

javascript - 所需的 Node/快速模块不使用 chokidar 更新?

转载 作者:行者123 更新时间:2023-11-29 21:06:11 25 4
gpt4 key购买 nike

我在这里阅读了很多关于 chokidar 的问题和答案,但我仍然感到困惑......所以非常感谢任何可以调试我的特定代码段的人。

我在 localhost:3000 运行一个基本的 Express Node 应用。

入口点是 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 并让更改反射(reflect)在下一页加载时?

作为引用,game.njk 模板如下所示:

{% extends "app.njk" %}

{% block pageContent %}
<div id="pageContent" class="container">
This. is. game. {{ also }}
</div><!-- /#pageContent .container -->
{% endblock pageContent %}

如果我更新模板,它会立即更新(根据 nunjucks watch: true 声明)但 var 更新也没有运气。

感谢任何帮助。

最佳答案

当您清除缓存时,您并没有更新 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 is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development. Install it using npm.

Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes. To install, get node.js, then from your terminal run:

npm install -g nodemon

然后:

nodemon server.js

关于javascript - 所需的 Node/快速模块不使用 chokidar 更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com