Grunt Livereload + Grunt Connect Proxy

我正在使用Rails作为我的API,AngularJS在前面,我遇到了一些问题,让livereload / grunt connect proxy正常工作。

这是我的gruntfile的片段:

connect: { options: { port: 9000, // Change this to '0.0.0.0' to access the server from outside. hostname: 'localhost', livereload: 35729 }, proxies: [ { context: '/api', host: 'localhost', port: 3000 } ], livereload: { options: { open: true, base: [ '.tmp', '' ], middleware: function (connect, options) { var middlewares = []; var directory = options.directory || options.base[options.base.length - 1]; // enable Angular's HTML5 mode middlewares.push(modRewrite(['!\\.html|\\.js|\\.svg|\\.css|\\.png$ /index.html [L]'])); if (!Array.isArray(options.base)) { options.base = [options.base]; } options.base.forEach(function(base) { // Serve static files. middlewares.push(connect.static(base)); }); // Make directory browse-able. middlewares.push(connect.directory(directory)); return middlewares; } } }, test: { options: { port: 9001, base: [ '.tmp', 'test', '' ] } }, dist: { options: { base: '' } } } 

如果我’咕噜咕噜’一切都很完美 – 离开localhost:3000

但是,如果我’咕噜咕噜’它会打开一个通过127.0.0.1:9000的窗口,我得到404所有的API调用。

同样在服务下它是从CSS文件中修改我的背景图像我收到此警告:

 Resource interpreted as Image but transferred with MIME type text/html: "http://127.0.0.1:9000/images/RBP_BG.jpg" 

我之前没有这样做过 – 所以很可能我做错了。

我不喜欢你的connect.livereload.middleware配置中的太多代码。
这一切都是必要的吗?

看看这个提交 – chore(yeoman-gruntfile-update): configured grunt-connect-proxy在我的一些项目中chore(yeoman-gruntfile-update): configured grunt-connect-proxy

  • 后端是Django
  • ports:frontend:9000,后端:8000
  • generator-angular项目时, generator-angular在v.0.6.0中
  • 我的connect.livereload.middleware配置基于: https : //stackoverflow.com/a/19403176/1432478

这是一篇旧post,但请确保在livereload之前通过调用configureProxies实际初始化grunt服务任务中的代理。

 grunt.task.run([ 'clean:server', 'bower-install', 'concurrent:server', 'autoprefixer', 'configureProxies', 'connect:livereload', 'watch' ]); 

之后应该工作得很好。

我和你有类似的问题,但我没有使用自己。

我的解决方案是添加任务’configureProxies’。

这是我的任务:

grunt.registerTask(’serve’,[‘connect:livereload’,’configureProxies’,’open:server’,’watch’]);

并且,’connect:livereload’,’configureProxies’ – 在我的测试之后,这两个任务的顺序不会影响结果。

github grunt-connect-proxy