使用nodemon一直重复运行 像吃了炫迈一样根本停不下来

下面是Bug源码

const fs = require('fs');
const path = require('path');

//定义正则表达式匹配script和style标签
const regStyle = /<style>[\s\S]*<\/style>/;
const regScript = /<script>[\s\S]*<\/script>/;

//读取文件
fs.readFile(path.join(__dirname, './index.html'), 'utf8', (err, data) => {
    if (err) {
        return console.log('读取文件失败' + err.message);
    } else {

        function resolve(htmlStr, reg, result) {
            // 1.用正则提取标签
            const r1 = reg.exec(htmlStr);
            // 2.将提取出来的样式进行处理
            const newStr = r1[0].replace(`<${result}>`, '').replace(`</${result}>`, '');
            // console.log(newStr);
            fs.writeFile(path.join(__dirname, `./bear/index.${result == 'style' ? 'css' : 'js'}`), newStr, err => {
                if (err) return console.log(err);
                console.log('写入文件成功');
            })
        };

        function merge(htmlStr) {
            const newHtml = htmlStr.replace(regStyle, '<link rel="stylesheet" href="index.css">').replace(regScript, '<script src="index.js"></script>');
            fs.writeFile(path.join('./bear/index.html'), newHtml, err => {
                if (err) return console.log(err);
                console.log('合并成功');
            })
        }
        resolve(data, regStyle, 'style');
        resolve(data, regScript, 'script')
        merge(data);
    }
});

程序没有报错 能成功运行

img

你这个只是一个普通的函数,并不是一个服务,不能用nodemon启动,不然会一直重启。 nodemon是用来守护一个服务的
你这个直接运行即可。