我的 meanjs 服务器需要 3-6 分钟才能启动

My meanjs server takes 3-6 minutes to start

我的 mean.js 应用基于 yoeman meanjs 生成器,并进行了一些调整(例如,将前端和后端分开,以便它们可以单独部署)。

我正在使用 fig 启动应用程序(请参阅下面的 fig.yml)。 当我将命令设置为 "node server.js" 时,服务器需要 6 秒才能启动。

当我使用 "grunt" 启动时,运行s nodemon 和 watch,大约需要 6 分钟。我尝试了各种方法,但无法真正理解为什么 nodemon 会导致 运行 慢得多

fig.yml:

web:
  build: .
  links:
   - db:mongo.local
  ports:
   - "3000:3000"
  volumes:
   - .:/home/abilitie
  command: grunt
  #command: node server.js # much faster but you don't get the restart stuff
  environment: 
   NODE_ENV: development
db:
  image: dockerfile/mongodb
  ports: 
   - "27017:27017"

G运行t文件(摘录)

concurrent: {
    default: ['nodemon', 'watch'],
    old_default: ['nodemon', 'watch'],
    debug: ['nodemon:debug', 'watch', 'node-inspector'],
    options: {
        logConcurrentOutput: true,
        limit: 10
    }
},

jshint: {
    all: {
        src: watchFiles.serverJS,
        options: {
            jshintrc: true
        }
    }
},

grunt.registerTask('lint', ['jshint']);
// Default task(s).
grunt.registerTask('default', ['lint', 'concurrent:default']);

这是因为您的第一种方法只是 运行 通过 $ 节点 server.js Express 服务器。但我不明白为什么我需要 6 秒才能开始?也许您的硬件速度较慢...

为了理解为什么第二种方法需要 6 分钟,您需要了解 grunt 启动后做了什么:

  1. 检查所有这些 JavaScript 个文件

    serverJS: ['gruntfile.js', 'server.js', 'config/**/*.js']
    clientJS: ['public/js/*.js', 'public/modules/**/*.js']
    
  2. 启动两个并行进程:watch & nodemon

    如果 watch 是清楚的(它从设置中监视文件并在编辑它们后重新启动服务器)nodemon 做什么?更准确地说,通过 nodejsnodemon.

  3. 启动服务器有什么区别

来自官方github documentation:

nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.

If you have a package.json file for your app, you can omit the main script entirely and nodemon will read the package.json for the main property and use that value as the app.

它正在监视 node_modules 目录中的所有文件,在我的 meanjs v0.4.0 中它有大约 41,000 个文件。在您的情况下,缓冲所有这些文件大约需要 6 分钟。尝试添加到您的 gruntfile.js grunt.initConfig > nodemon > dev > option ignore

    nodemon: {
        dev: {
            script: 'server.js',
            options: {
                nodeArgs: ['--debug'],
                ext: 'js,html',
                watch: watchFiles.serverViews.concat(watchFiles.serverJS),
                ignore: 'node_modules/*' // or '/node_modules'
            }
        }
    },

您需要准确判断问题出在哪里。尝试通过三种不同的方式启动服务器并测量时间

  1. NODE_ENV=development nodejs server.js
  2. NODE_ENV=development nodemon server.js
  3. NODE_ENV=development nodemon server.js --ignore node_modules/

NFS 挽救了局面。

VirtualBox 共享文件夹超慢。使用这个 vagrant 镜像代替 boot2docker 会快得多。

https://vagrantcloud.com/yungsang/boxes/boot2docker

此外,请确保禁用 UDP,否则 NFS 可能会挂起。您可以将其放入您的 Vagrantfile 中:

config.vm.synced_folder ".", "/vagrant", type: "nfs", nfs_udp: false