如何将live-reload添加到我的nodejs服务器

Ahm*_*bai 13 node.js express livereload reactjs

这是我如何运行我的服务器nodejs,当我对前端dev中的代码进行更改时,我需要liverealord我的服务器

"start": "node server.js"
Run Code Online (Sandbox Code Playgroud)

psp*_*spi 11

您可以使用“livereload”、“connect-livereload”和“nodemon”包将前端和后端更改实时重新加载到浏览器。这样你就不需要Gulp了。以下是这些包的组合方式:

  • livereload打开高端口并通知浏览器公共文件发生变化
  • connect-livereloadMonkey 使用连接到此高端口的代码片段修补每个提供的 HTML 页面
  • nodemon在更改的后端文件上重新启动服务器

在 Express 中设置 livereload

设置 Express 以启动 livereload 服务器来监视公共目录并在nodemon诱导重启期间对浏览器执行 ping 操作:

const livereload = require("livereload");
const connectLivereload = require("connect-livereload");

// open livereload high port and start to watch public directory for changes
const liveReloadServer = livereload.createServer();
liveReloadServer.watch(path.join(__dirname, 'public'));

// ping browser on Express boot, once browser has reconnected and handshaken
liveReloadServer.server.once("connection", () => {
  setTimeout(() => {
    liveReloadServer.refresh("/");
  }, 100);
});

const app = express();

// monkey patch every served HTML so they know of changes
app.use(connectLivereload());
Run Code Online (Sandbox Code Playgroud)

使用 nodemon 启动 Express

使用 nodemon 启动服务器,例如使用专用的监视脚本npm run watch

这里的关键点是忽略已经被 livereload 监视的公共目录。您还可以配置要观看的具有非默认扩展名的文件,例如 pug 和 Mustache。

"scripts": {
  "start": "node ./bin/www",
  "watch": "nodemon --ext js,pug --ignore public"
},
Run Code Online (Sandbox Code Playgroud)

您可以在“使用 Express、LiveReload 和 Nodemon 刷新浏览器的前端和后端更改”中阅读更长的说明。


Mar*_*sel 6

第一:

npm install -g nodemon
Run Code Online (Sandbox Code Playgroud)

接下来将脚本行添加到package.json

"live": "nodemon server.js" 
Run Code Online (Sandbox Code Playgroud)

现在,当您使用npm live时,它将重新加载

有关更多详细信息,请参见https://github.com/remy/nodemon

如果还需要重新加载实时页面,则更新

npm install -g livereload
livereload . -w 1000 -d
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参见https://github.com/napcs/node-livereload

  • 这不会实时重新加载浏览器,而只是实时重新加载服务器。您仍然需要手动刷新页面。 (20认同)
  • 来自寻找真正的实时重新加载的人的注释:这实际上不是服务器的实时重新加载,这是自动重新启动,这是一个非常不同的事情。此解决方案杀死节点进程,并启动恰好在同一端口上运行的_new_进程。如果有人登录到服务器,或者服务器本身已针对某些外部 API 进行了身份验证,则此解决方案将不会为您提供所需的实时重新加载。 (3认同)
  • 问题是“我需要使用liverealord我的服务器”而不是实时重新加载我的浏览器 (2认同)
  • 提问者提到了他对前端开发人员的需求。对于初学者来说,这种混乱似乎是合理的。 (2认同)

Mur*_*men 5

npm install browser-refresh -g
Run Code Online (Sandbox Code Playgroud)

并添加您的主要js

 if (process.send) {
     process.send('online');
 }
Run Code Online (Sandbox Code Playgroud)

例如

app.listen(port, function() {
    console.log('Listening on port %d', port);

    if (process.send) {
        process.send('online');
    }
});
Run Code Online (Sandbox Code Playgroud)

并在正文结束标记之前添加您的索引页。

<script src="{process.env.BROWSER_REFRESH_URL}"></script>
Run Code Online (Sandbox Code Playgroud)

并在终端而不是节点 server.js 上启动您的服务器

browser-refresh server.js
Run Code Online (Sandbox Code Playgroud)


Alo*_*nad 5

重新启动服务器是一回事,刷新浏览器是另一回事。对于服务器观看,我使用nodemon。Nodemon可以查看任何类型的文件何时发生更改。但是nodemon无法刷新浏览器页面。为此,我使用浏览器同步。

我都用了。

因此,请使用package.json的依赖项使其起作用:

"devDependencies": {
"browser-sync": "^2.24.5",
"gulp": "^3.9.1",
"gulp-nodemon": "^2.2.1"
}
Run Code Online (Sandbox Code Playgroud)

在服务器文件中(我的服务器在./bin/www中,您的服务器可以在server.js,app.js或其他位置),快速服务器侦听端口3001。

var port = normalizePort(process.env.PORT || '3001');
var server = http.createServer(app);
server.listen(port);
Run Code Online (Sandbox Code Playgroud)

接下来是在gulp中运行nodemon和浏览器同步。gulpfile.js的全部内容

var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var browserSync = require('browser-sync').create();

gulp.task('gulp_nodemon', function() {
  nodemon({
    script: './bin/www', //this is where my express server is
    ext: 'js html css', //nodemon watches *.js, *.html and *.css files
    env: { 'NODE_ENV': 'development' }
  });
});

gulp.task('sync', function() {
  browserSync.init({
    port: 3002, //this can be any port, it will show our app
    proxy: 'http://localhost:3001/', //this is the port where express server works
    ui: { port: 3003 }, //UI, can be any port
    reloadDelay: 1000 //Important, otherwise syncing will not work
  });
  gulp.watch(['./**/*.js', './**/*.html', './**/*.css']).on("change", browserSync.reload);
});

gulp.task('default', ['gulp_nodemon', 'sync']);
Run Code Online (Sandbox Code Playgroud)

在终端中运行gulp时,它将开始监视服务器以及在任何文件更改时刷新浏览器。

尽管我们在快递服务器中指定了端口3001,但我们在浏览器同步中编写的应用仍将在端口3002上运行。3001将用作代理。

  • 我似乎无法在最新的 gulp v4 中使用它,尽管它在 gulp v3 中运行良好。您能建议哪些内容需要更新/更改吗? (2认同)
  • 我使用此处的注释制作了一个小样板,以防您想要从一开始就可以使用的东西,代码[此处](https://github.com/dangel29/express-hot-reload)。额外的好处是,它也已经准备好了。它使用nodemon、gulp和browsersync,但针对Gulp 4进行​​了更新。除了最后一行之外,与上面的代码基本相同: `exports.build = gulp.parallel(["gulp_nodemon", "sync"]);` 编写导出.build 使“gulp build”命令在您的 npm 脚本中可用。 (2认同)