【问题标题】:getting Cannot GET / while using Grunt Connect使用 Grunt Connect 时无法 GET /
【发布时间】:2016-02-26 19:20:24
【问题描述】:

在使用 grunt 运行服务器时,出现无法获取的错误 /

server.js

    var express = require('express');

    var app = express();
    var port = process.env.PORT || 2323;

    app.use(express.static(__dirname + '/server'));
    app.use(express.static(__dirname + '/public'));

    app.get('/', function(req, res){
        res.sendfile('./server/views/index.html');
    });

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

Gruntfile.js

module.exports = function(grunt){

    var modRewrite = require('connect-modrewrite');
    var serveStatic = require('serve-static');

    grunt.initConfig({

        connect: {
            server: {
                options: {
                    port: 2323,
                    base: 'views',
                    keepalive: true,
                    livereload: 35729,
                    hostname: 'localhost',
                    open: true,
                    middleware: function (connect) {
                        return [
                                    modRewrite(['^[^\\.]*$ /index.html [L]']),
                                    connect().use('/', serveStatic('/views'))
                                ];
                            }
                   }
            }
        }

        // configure connect



    });

    //load nodemon
    grunt.loadNpmTasks('grunt-contrib-connect');

    //register the nodemon task when we run grunt
    grunt.registerTask('default', ['connect:server']);

};

而且我不知道我的 grunt-file 代码是对还是错 实际上是新的 grunt,我不知道该怎么做,运行 grunt 时遇到 Cannot GET / 这个错误。

【问题讨论】:

    标签: angularjs node.js gruntjs


    【解决方案1】:

    你只是在 Grunt 中运行的连接服务器的设置出错了:

    connect().use('/', serveStatic('/views'))
    

    应该是

    connect().use('/', serveStatic('./server/views'))
    

    【讨论】:

    • 如果你想让它表现得像 server.js,你需要在 Gruntfile 中设置一个连接服务器。你可能最好使用grunt-express,因为看起来你更习惯于表达。
    猜你喜欢
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2015-01-25
    • 2013-12-28
    相关资源
    最近更新 更多