使用grunt-contrib-connect和grunt-connect-rewrite删除文件扩展名

jam*_*son 6 javascript mod-rewrite node.js gruntjs

我正在尝试从我的grunt网络应用程序中的文件中删除".html".

http://testing.com/one/应从该文件夹返回index.html,但如果没有斜杠(http://testing.com/one),则应检查one.html

grunt-connect-rewrite似乎可以正常使用我能找到的示例,但从.html文件中删除文件扩展名似乎会让我感到害怕.这里的规则类似于我在.htaccess文件中使用的规则.

connect: {
    server: {
        options: {
            port: 9000,
            keepalive: true,
            base: 'dist',
            middleware: function(connect, options) {
              return [
                rewriteRulesSnippet, 
                // Serve static files
                connect.static(require('path').resolve(options.base))
              ];
            }
        },
        rules: {
            '^(.*)\.html$': '/$1'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以问题是,这里使用的正确规则是什么?

Mil*_*los 5

答案对我没有用,所以我一直玩弄它直到找到解决方案.

正则表达式:

from: '(^((?!css|html|js|img|fonts|\/$).)*$)',
to: "$1.html"
Run Code Online (Sandbox Code Playgroud)

包装版本:

"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-connect": "~0.5.0",
"grunt-connect-rewrite": "~0.2.0"
Run Code Online (Sandbox Code Playgroud)

完整的工作Gruntfile:

var rewriteRulesSnippet = require("grunt-connect-rewrite/lib/utils").rewriteRequest;
module.exports = function(grunt) {
  grunt.initConfig({
    watch: {
      html: {
        files: "**/*.html"
      }
    },
    connect: {
      options: {
        port: 9000,
        hostname: "127.0.0.1"
      },
      rules: [{
        from: '(^((?!css|html|js|img|fonts|\/$).)*$)',
        to: "$1.html"
      }],
      dev: {
        options: {
          base: "./",
          middleware: function(connect, options) {
            return [rewriteRulesSnippet, connect["static"](require("path").resolve(options.base))];
          }
        }
      },
    }
  });
  grunt.loadNpmTasks("grunt-connect-rewrite");
  grunt.loadNpmTasks("grunt-contrib-connect");
  grunt.loadNpmTasks("grunt-contrib-watch");
  grunt.registerTask("default", ["configureRewriteRules", "connect:dev", "watch"]);
};
Run Code Online (Sandbox Code Playgroud)