配置nodemon刷新相同的选项卡 [英] Configure nodemon to refresh same tab
问题描述
这可能真是个菜鸟.我想知道是否有一种方法可以配置nodemon来刷新同一标签,而不是每次在js文件中进行更改时都打开一个新标签.
This could be really rookie. I want to know if there is a way that I can configure nodemon to refresh the same tab instead of opening a new tab each time I make a change in my js files.
推荐答案
我使用了一个名为reload的程序包.假设您正在FE上执行此操作,并且已经安装了express.js和nodemon 首先安装重新加载
I use a package called reload. Assuming you are doing this on your FE and that you already installed express.js and nodemon first install reload
npm install --save-dev reload
比创建index.js文件或server.js或您想要的任何名称
Than create a index.js file or server.js or whatever name your want
这是我的index.js:
this is my index.js:
const express = require('express')
const http = require('http')
const reload = require('reload')
const opn = require('opn')
const app = express()
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')
app.set('src', './src')
app.use(express.static('src'))
app.get('/', (req, res) => res.render('index'))
const server = http.createServer(app)
server.listen(8080, function() {
console.log('Listening to port 8080...')
})
opn('http://localhost:8080')
reload(app)
opn软件包可以与
npm install opn
一旦在终端上键入npm start,它将自动打开您的本地主机
it will automatically open your localhost once you type npm start on your terminal
在HTML上,您必须在结束body标记后插入以下内容:
on HTML you have to insert something like this after the closing body tag:
</body>
<script src="/reload/reload.js"></script>
并在package.json上执行以下操作:
and on package.json the following:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js -e els,js,html,css,json"
},
唯一的问题是,每次更改这些文件时,它将重新打开浏览器,并将当前打开的文件重新加载到opn软件包中.最终,我仍将创建一个函数来防止这种情况的发生.希望有帮助!
The only problem with this is that it will reopen the browser every time you make a change to those files and well as reload the current opened one sure to the opn package. I am still going to create a function to prevent that, eventually. Hope that helps!
这篇关于配置nodemon刷新相同的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!