grunt connect proxy regexp example

// The actual grunt server settings
connect: {
    options: {
        port: 9000,
        livereload: 35729,
        // Change this to '0.0.0.0' to access the server from outside
        // hostname: 'localhost'
        hostname: '0.0.0.0'
    },
    // proxy配置
    proxies: [{
        context: '/cortex',
        host: '0.0.0.0',
        port: 80,
        https: false
    }],
    // proxy
    server: {
        options: {
            open: 'http://localhost',
            base: [
                '<%= yeoman.dist %>',
                '<%= yeoman.tmp %>',
                '<%= yeoman.app %>'
            ],
            middleware: function (connect, options, middlewares) {
                middlewares.push(
                    function (req, res, next) {
                        var fs = require('fs');
                        var path = require('path');
                        var mimeType = {
                            "css": "text/css",
                            "gif": "image/gif",
                            "html": "text/html",
                            "ico": "image/x-icon",
                            "jpeg": "image/jpeg",
                            "jpg": "image/jpeg",
                            "js": "text/javascript",
                            "json": "application/json",
                            "pdf": "application/pdf",
                            "png": "image/png",
                            "svg": "image/svg+xml",
                            "swf": "application/x-shockwave-flash",
                            "tiff": "image/tiff",
                            "txt": "text/plain",
                            "wav": "audio/x-wav",
                            "wma": "audio/x-ms-wma",
                            "wmv": "video/x-ms-wmv",
                            "xml": "text/xml"
                        };

                        // just redirect static files to local workspace
                        if (!/\.(js|html|css|png)/g.test(req.url)) {
                            next();
                            return;
                        }

                        var pathname = req._parsedUrl.pathname;
                        var ext = path.extname(pathname);
                        ext = ext ? ext.slice(1) : 'unknown';
                        var contentType = mimeType[ext] || "text/plain";
                        pathname = path.resolve(process.cwd(), '.') + pathname;

                        fs.exists(pathname, function (exists) {
                            if (!exists) {
                                res.writeHead(404, {
                                    'Content-Type': 'text/plain'
                                });
                                res.write("This request URL " + pathname + " was not found on this server.");
                                res.end();
                            } else {
                                fs.readFile(pathname, "binary", function (err, file) {
                                    if (err) {
                                        res.writeHead(500, {
                                            'Content-Type': 'text/plain'
                                        });
                                        res.end(err);
                                    } else {
                                        res.writeHead(200, {
                                            'Content-Type': contentType
                                        });
                                        res.write(file, "binary");
                                        res.end();
                                    }
                                });
                            }
                        });
                    }
                );

                // Setup the proxy
                middlewares.push(require('grunt-connect-proxy/lib/utils').proxyRequest);
                return middlewares;
            }
        }
    }
}

// other code
grunt.registerTask('serve', function (target) {
    grunt.task.run([
        'clean:dist',
        'less',
        'stylus',
        'configureProxies:server',
        'connect:server',
        'watch'
    ]);
});

ie9在服务器重定向时会丢失hash值的bug

高版本浏览器在服务器重定向时会带上原始链接中的hash值,某些低版本浏览器如IE9,则只会带上重定向时location中的hash值。因此对于单页面应用,某些情况下会出错。如对于nginx重定向http到https这种场景,如果location中没有带上原始数据的hash值,则就会有问题。

参考链接:

URL Fragment and 302 redirects

IE9 redirect test page