Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node文件更新自动重载 #6

Open
Liqihan opened this issue May 11, 2017 · 0 comments
Open

node文件更新自动重载 #6

Liqihan opened this issue May 11, 2017 · 0 comments
Labels

Comments

@Liqihan
Copy link
Owner

Liqihan commented May 11, 2017

文件更新自动重载

const fs = require('fs'),
    path = require('path'),
    projectRootPath = path.resolve(__dirname, './src');

const watch = project => {
    require('./src'); // 启动 APP,自动检索到 src/index.js
    try { // 监听文件夹
        fs.watch(project, { recursive: true }, cacheClean)
    } catch(e) {
        console.error('watch file error');
    }
}
// 清除缓存
const cacheClean = () => {
    Object.keys(require.cache).forEach(function (id) {
        if (/[\/\\](src)[\/\\]/.test(id)) {
            delete require.cache[id]
        }
    })
}
// 启动开发模式
watch(projectRootPath);

or

const pm2 = require('pm2');

pm2.connect(function(err) {
    if (err) {
        console.error(err);
        process.exit(2);
    }

    pm2.start({
        "watch": ["./app"], // 开启 watch 模式,并监听 app 文件夹下的改动
        "ignore_watch": ["node_modules", "assets"], // 忽略监听的文件
        "watch_options": {
            "followSymlinks": false // 不允许符号链接
        },
        name: 'httpServer',
        script: './server/index.js', // APP 入口
        exec_mode: 'fockMode', // 开发模式下建议使用 fockModel
        instances: 1, // 仅启用 1 个 CPU
        max_memory_restart: '100M' // 当占用 100M 内存时重启 APP
    }, function(err, apps) {
        pm2.disconnect(); // Disconnects from PM2
        if (err) throw err
    });
});
@Liqihan Liqihan added the blog label Oct 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant