yeoman如何设置标题(.htaccess?)

时间:2013-01-31 01:42:39

标签: node.js yeoman gruntjs

默认情况下,运行 yeoman服务器似乎无法识别.htaccess文件。是否还有其他步骤可以读取.htaccess文件?

以下是我取消注释的行,重启后设置标题没有明显的影响:

    # ----------------------------------------------------------------------
    # Cross-domain AJAX requests
    # ----------------------------------------------------------------------

    # Serve cross-domain Ajax requests, disabled by default.
    # enable-cors.org
    # code.google.com/p/html5security/wiki/CrossOriginRequestSecurity

    <IfModule mod_headers.c>
      Header set Access-Control-Allow-Origin "*"
    </IfModule>

或许正确的问题是在运行yeoman服务器时如何设置标头?还有其他选择,可能在Gruntfile.js中吗?

1 个答案:

答案 0 :(得分:22)

grunt server只是一个Node.js connect服务器,不支持.htaccess

虽然grunt-contrib-connect支持自定义中间件,但您可以将其添加到Gruntfile.js中:

var corsMiddleware = function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  next();
}

grunt.initConfig({
  connect: {
    server: {
      options: {
        middleware: function(connect, options) {
          return [
            // Serve static files
            connect.static(options.base),
            // Make empty directories browsable
            connect.directory(options.base),
            // CORS support
            corsMiddleware
          ];
        }
      }
    }
  }
});