You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grunt Serve返回Cannot GET/,ModRewrite配置无效求助

Grunt Serve 显示 "cannot GET/",配置 modRewrite 后问题依旧

我刚接触Grunt,这周五才开始学习。运行grunt serve命令启动服务后,浏览器打开的页面只显示**"cannot GET/"**,完全看不到项目内容。我查了资料尝试用modRewrite修复,按教程配置了中间件,但问题还是没解决,恳请各位大佬帮忙看看哪里出了问题!

以下是我的Gruntfile完整代码:

/* global module:true * * Gruntfile.js * npm install -g grunt-cli * npm install grunt-contrib-less grunt-contrib-watch grunt-contrib-connect --save-dev */
module.exports = function(grunt) {
'use strict';
var serveStatic = require('serve-static');
var phpMiddleware = require('connect-php');
var modRewrite = require('connect-modrewrite')
grunt.initConfig({
connect: {
server: {
options: {
port: 8765,
livereload: 35729,
open: true,
middleware: function(connect, options) {
var middlewares;
middlewares = [];
middlewares.push(
modRewrite( ['^[^\\.]*$ /index.html [L]'] )
);
var directory = options.directory || options.base[options.base.length - 1];
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
// Magic happens here
middlewares.push(phpMiddleware(directory));
options.base.forEach(function(base) {
// Serve static files.
middlewares.push(serveStatic(base));
});
// Make directory browse-able.
//middlewares.push(connect.directory(directory));
return middlewares;
}
}
}
},
// Less files are in app/less, output is in app/css
less: {
development: {
options: {
paths: ["./less"],
yuicompress: false
},
files: {
"./css/style.css": "./less/style.less"
}
}
},
watch: {
options: {
livereload: 35729
},
files: "./less/*.less",
tasks: ["less"]
},
uglify: {
dist: {
options: {
sourceMap: 'js/map/source-map.js'
},
files: {
'js/plugins.min.js': [
'js/source/plugins.js',
'js/vendor/**/*.js',
'js/vendor/modernizr*.js'
],
'js/main.min.js': [
'js/source/main.js'
]
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-uglify');
// Run grunt server to get going
grunt.registerTask('serve', [ 'connect', 'watch' ]);
grunt.registerTask('server', function () {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
grunt.task.run(['serve']);
});
};

内容的提问来源于stack exchange,提问作者Valentijn Veldhoen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:58:08