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

M J*_*M J 7 node.js gruntjs yeoman

默认情况下,运行 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中?

Sin*_*hus 22

grunt server只是一个Node.js 连接服务器,不支持.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
          ];
        }
      }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)