drewzboto / grunt-connect-proxy

Grunt Connect support for proxying API calls during development
MIT License
424 stars 122 forks source link

Add regular expression support/wildcard support to context #86

Open roc opened 9 years ago

roc commented 9 years ago

It would be great to get some support for regex/wildcard matching in the context.

So for example

context: [ '/api/houses/*/rooms' ]

— where * is whatever house ID we have at that point.

This would be really useful when you want to proxy '/api/houses/*/rooms' but don't want to proxy e.g. '^/api/houses/'

elsigh commented 9 years ago

+1

LeeAdcock commented 9 years ago

+1 or to use regex to specify all urls except those that match

ruiaraujo commented 9 years ago

Feel free to send a PR.

jers73 commented 9 years ago

+1

aflext commented 9 years ago

hi,guys check this:

        // 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'
            ]);
    });
maksimr commented 9 years ago

+1

glebv commented 8 years ago

+1

kg782 commented 8 years ago

+1

thejeff77 commented 5 years ago

+1