如何通过 g运行t 运行 node.js 应用程序?

How to run node.js app by grunt?

文件夹结构:-

我的应用程序

--public(目录) //这里是与public(Angularjs,css,etc)

相关的文件

--server(directory) //这里是server相关的文件

--server.js

代码

server.js //at root directory
----------------------
...
var app = express();
app.disable('x-powered-by');

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public')); // set the static files location
app.use('/profilepic', express.static(__dirname + '/uploads/profilepic')); // set the static files location
app.use('/company', express.static(__dirname + '/uploads/company'));
app.use('/download', express.static(__dirname + '/uploads/download'));
app.use(morgan('dev'));

app.use(cookieParser());
app.use(expressSession({
    secret: 'secret',
    resave: true,
    saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
require('./server/config/passport')(app, passport);
require('./server/route')(app, passport);
app.use(flash());
if (config.env === 'development') {

    app.use(function(err, req, res, next) {
        console.log(err);
        return res.status(500).json({ message: err.message });
    });

}

app.use(function(err, req, res, next) {
    return res.status(500).json({ message: err.message });
});


app.listen(config.port, function() {
    console.log('app listen on ' + config.port);
});





Gruntfile.js //at root directory
-----------------
module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        jshint: {
            options: {
                reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
            },

            // when this task is run, lint the Gruntfile and all js files in src
            build: ['Grunfile.js', './']
        },
        ngAnnotate: {
            options: {
                singleQuotes: true
            },
            app: {
                files: {
                    './dist/min-safe/services.js': ['./public/scripts/services/*.js'],
                    './dist/min-safe/filters.js': ['./public/scripts/filters/*.js'],
                    './dist/min-safe/directives.js': ['./public/scripts/directives/*.js'],
                    './dist/min-safe/controllers.js': ['./public/scripts/controllers/*.js'],
                    './dist/min-safe/app.ctrl.js': ['./public/scripts/app.ctrl.js'],
                    './dist/min-safe/config.router.js': ['./public/scripts/config.router.js'],
                    './dist/min-safe/config.lazyload.js': ['./public/scripts/config.lazyload.js'],
                    './dist/min-safe/config.js': ['./public/scripts/config.js'],
                    './dist/min-safe/app.js': ['./public/scripts/app.js']
                }
            }
        },
        concat: {
            js: { //target
                src: ['./dist/min-safe/app.js', './dist/min-safe/config.js',
                    './dist/min-safe/config.lazyload.js', './dist/min-safe/config.router.js',
                    './dist/min-safe/app.ctrl.js', './dist/min-safe/controllers.js',
                    './dist/min-safe/directives.js', './dist/min-safe/filters.js', './dist/min-safe/services.js'
                ],
                dest: './dist/min/lms.js'
            }
        },
        copy: {
            main: {
                expand: true,
                src: './dist/min/lms.js',
                dest: './public',
                flatten: true,
                filter: 'isFile'
            }
        },
        connect: {
            options: {
                base: 'xxxxxxxx',
                keepalive: true
            }
        },

    });

    // Load the plugin that provides the "uglify" task.
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-ng-annotate');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-copy');

    // Default task(s).
    grunt.registerTask('default', ['ngAnnotate', 'concat', 'copy']);
    grunt.registerTask('server', ['default', 'connect']);
    grunt.registerTask('jshint', ['jshint']);

};

我 运行 应用程序由 node server.js 但现在我想 运行 这个应用程序由 g运行t 和当我 运行 grunt server 它抛出 No "connect" targets found 错误。

如何将 g运行t 文件配置到 运行 应用程序?

When i run grunt server the it throw No "connect" targets found error. How to configure grunt file to run app?

在您的 Gruntfile.js 中更新 connect Task to include a Target 如下:

// ...
connect: {
    server: { // <-- This is a Target named 'server'.
        options: {
            // <-- Your connect options go here.
            //     https://github.com/gruntjs/grunt-contrib-connect#options
        }
    }
}

接下来将您名为 servergrunt.registered.Task() 更新为:

grunt.registerTask('server', ['default', 'connect:server']);

注: 而不是在 taskList Array 中给 connect 任务取别名现在使用语法 'connect:server'

connect 任务中引用 server 目标

有关 grunt.registered.Task() 的更多信息,请参见 Creating Tasks


编辑

根据评论中的反馈和对原始 post/question 所做的编辑进行了更新:

假设通过您的 CLI 运行ning $ node server.js 按预期工作,您可以通过 g运行t.

运行 相同的命令
  1. 卸载 grunt-contrib-connect 运行ning:

$ npm un -D grunt-contrib-connect

  1. Gruntfile.js

  2. 中删除读取 grunt.loadNpmTasks('grunt-contrib-connect'); 的代码行
  3. Gruntfile.js

  4. 中删除connect任务
  5. 运行下面安装load-grunt-tasks and grunt-shell:

npm i -D load-grunt-tasks grunt-shell

  1. 将以下任务添加到 Gruntfile.js
// ...
shell: {
    connect: {
        command: 'node server.js'
    }
}
// ...
  1. 将当前的grunt.registerTask()更改为:
grunt.registerTask('server', ['default', 'shell:connect']);
  1. 将以下代码行添加到 Gruntfile.js:
require('load-grunt-tasks')(grunt);
  1. 运行 $ grunt server 并访问 localhost:3000