drewzboto / grunt-connect-proxy

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

use grunt-connect-proxy forward from port 80 to 3000 #75

Open rshailu opened 9 years ago

rshailu commented 9 years ago

After install and running mean.js on my linux vm, I wanted to use mycompany.com to forward to ipaddress:3000 using grunt-connect-proxy. I get error "connection refused", even when I do curl on the same linux vm. Thinking it might be due to incorrect configuration of my DNS registration, I did try with ipaddress, still no luck. Can some one please help me with proxy forwarding with mean and grunt. I did go through documentation of grunt-connect-proxy and its not clear enough. I must be making some simple mistake in my grunt configuration, here is the gruntfile.js content,

var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

var lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;

module.exports = function(grunt) {

// Unified Watch Object

var watchFiles = {

serverViews: ['app/views//.'],

serverJS: ['gruntfile.js', 'server.js', 'config/*/.js', 'app/*/.js'],

clientViews: ['public/modules//views//*.html'],

clientJS: ['public/js/_.js', 'public/modules/_/.js'],

clientCSS: ['public/modules/*/.css'],

mochaTests: ['app/tests/*/.js']

};

// Project Configuration

grunt.initConfig({

pkg: grunt.file.readJSON('package.json'),

connect: {

server: {

                                        options: {

                                                port: 80,

                                                hostname: '0.0.0.0'

                                        },

                                        proxies: [

                                                        {

                                                        context: '/',

                                                        host: 'localhost:3000',

                                                        changeOrigin: true

                                                        }

                                                ]

},

livereload: {

options: {

                                                  open: 'http://localhost:3000',

middleware: function (connect) {

return [

lrSnippet,

mountFolder(connect, '.tmp'),

mountFolder(connect, yeomanConfig.app),

proxySnippet

];

}

}

}

},

forever: {

  server1: {

options: {

index: 'server.js',

logDir: 'logs'

}

 }

},

watch: {

serverViews: {

files: watchFiles.serverViews,

options: {

livereload: true

}

},

serverJS: {

files: watchFiles.serverJS,

tasks: ['jshint'],

options: {

livereload: true

}

},

clientViews: {

files: watchFiles.clientViews,

options: {

livereload: true,

}

},

clientJS: {

files: watchFiles.clientJS,

tasks: ['jshint'],

options: {

livereload: true

}

},

clientCSS: {

files: watchFiles.clientCSS,

tasks: ['csslint'],

options: {

livereload: true

}

}

},

jshint: {

all: {

src: watchFiles.clientJS.concat(watchFiles.serverJS),

options: {

jshintrc: true

}

}

},

csslint: {

options: {

csslintrc: '.csslintrc',

},

all: {

src: watchFiles.clientCSS

}

},

uglify: {

production: {

options: {

mangle: false

},

files: {

'public/dist/application.min.js': 'public/dist/application.js'

}

}

},

cssmin: {

combine: {

files: {

'public/dist/application.min.css': '<%= applicationCSSFiles %>'

}

}

},

nodemon: {

dev: {

script: 'server.js',

options: {

nodeArgs: ['--debug'],

ext: 'js,html',

watch: watchFiles.serverViews.concat(watchFiles.serverJS)

}

}

},

'node-inspector': {

custom: {

options: {

'web-port': 1337,

'web-host': 'localhost',

'debug-port': 5858,

'save-live-edit': true,

'no-preload': true,

'stack-trace-limit': 50,

'hidden': []

}

}

},

            ngmin: {

                production: {

                    files: {

                        'public/dist/application.js': '<%= applicationJavaScriptFiles %>'

                    }

                }

            },

concurrent: {

default: ['nodemon', 'watch'],

debug: ['nodemon', 'watch', 'node-inspector'],

options: {

logConcurrentOutput: true

}

},

env: {

test: {

NODE_ENV: 'test'

}

},

mochaTest: {

src: watchFiles.mochaTests,

options: {

reporter: 'spec',

require: 'server.js'

}

},

karma: {

unit: {

configFile: 'karma.conf.js'

}

}

});

// Load NPM tasks

require('load-grunt-tasks')(grunt);

// Making grunt default to force in order not to break the project.

grunt.option('force', true);

// A Task for loading the configuration object

grunt.task.registerTask('loadConfig', 'Task that loads the config into a grunt option.', function() {

var init = require('./config/init')();

var config = require('./config/config');

grunt.config.set('applicationJavaScriptFiles', config.assets.js);

grunt.config.set('applicationCSSFiles', config.assets.css);

});

    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.loadNpmTasks('grunt-connect-proxy');

    grunt.loadNpmTasks('grunt-forever');

// Default task(s).

grunt.registerTask('default', ['lint', 'concurrent:default','configureProxies', 'connect:livereload']);

// Debug task.

grunt.registerTask('debug', ['lint', 'concurrent:debug']);

// Lint task(s).

grunt.registerTask('lint', ['jshint', 'csslint']);

// Build task(s).

grunt.registerTask('build', ['lint', 'loadConfig', 'ngmin', 'uglify', 'cssmin']);

// Test task.

grunt.registerTask('test', ['env:test', 'mochaTest', 'karma:unit']);

pdf commented 9 years ago

I didn't read all your config because the formatting is completely broken (wrap code three backticks (```), or indent it all with three spaces to fix the formatting). But, you can't bind to a port lower than 1024 as a non-root user, and it's an absolutely terrible idea to run Grunt as root, as it's also a bad idea to run it as your production server. Look at getting someone to help you set up a real web server.

mnishihan commented 9 years ago

Instead of using host: 'localhost:3000', in your proxy configuration, use host: 'localhost', port: 3000 and it should work.