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

时间:2015-01-30 05:04:14

标签: node.js gruntjs grunt-contrib-watch meanjs nodemon

我的mean.js app基于yoeman meanjs生成器,有一些调整(例如分离前端和后端,以便它们可以单独部署)。

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

当我使用运行nodemon并观看的“grunt”启动时,大约需要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"

Gruntfile(摘录)

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']);

2 个答案:

答案 0 :(得分:1)

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

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

enter image description here

  1. 提取所有这些JavaScript文件

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

    如果watch已清除(它正在查看来自stetting的文件并在编辑后重新启动服务器)nodemon是什么?更确切地说,通过nodejsnodemon启动服务器之间的区别是什么。

  3. 来自官方github documentation

      

    nodemon将监视启动nodemon的目录中的文件,如果有任何文件发生更改,nodemon将自动重启节点应用程序。

         

    如果您的应用程序有package.json个文件,则可以完全省略主脚本,nodemon将读取主要属性的package.json,并将该值用作应用程序。

    它正在查看node_modules目录中的所有文件以及我的meanjs v0.4.0中的~41,000个文件。在您的情况下,缓冲所有这些文件大约需要6分钟。尝试添加到您的gruntfile.js grunt.initConfig> nodemon> dev>选项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/

答案 1 :(得分:0)

NFS节省了一天。

VirtualBox共享文件夹超级慢。使用这个vagrant图像而不是boot2docker要快得多。

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

此外,请确保禁用UDP,否则NFS可能会挂起。你可以把它放在你的Vagrantfile中来实现:

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