Grunt-connect-proxy Resolving cross-domain issues during development

Source: Internet
Author: User

The most recent project in the front and back is completely decoupled, with grunt management of the project. This can lead to a problem: cross-domain issues occur when the front-end interface is not on a server because it is being called at development time. However, it is not possible to implement a true cross-domain in either JSONP or Cros, as the project is actually published under the same server.

At this time our grunt-connect-proxy on the stage, it is specifically to solve the problem.

Specific configuration:

1. Download and install this component first

NPM Install Grunt-connect-proxy--save-dev

Here to note: Be sure to first install grunt-contrib-livereload this component, or grunt serve will always error

2. Add Configuration

var lrsnippet = require (' grunt-contrib-livereload/lib/utils '). Livereloadsnippet; var function (Connect, dir) {    return connect.static (Require (' path '). Resolve (dir));}; var proxysnippet = require (' Grunt-connect-proxy/lib/utils '). Proxyrequest;

This break code should be added to the top of the gruntfile.js, module.exports above.

Then add proxy configuration and livereload configuration to connect

Connect: {options: {port:9000, open:true, Livereload:35729,        //Change the "0.0.0.0" to access the server from outsideHostname: ' localhost '}, proxies: [{context:'/WebSite ', Host:' Www.somesite.com ', Port:80, https:false, Changeorigin:true}], Livereload: {options: {middleware :function(connect) {return[Lrsnippet, Mountfolder (Connect,'. tmp '), connect (). Use ('/bower_components ', connect.static ('./bower_components ')) , Mountfolder (Connect, Config.app), Proxysnippet,                    ]; }        }      },/***/}

Next, add proxy to this task serve

Grunt.task.run ([      ' clean:server      ', ' WIREDEP ',      ' Concurrent:server ',       ' autoprefixer ',      ' configureproxies ',     // added to Livereload front '      connect:livereload ',      ' watch '    ]);

OK, here is the agent added!

Reference:

Https://github.com/drewzboto/grunt-connect-proxy

http://fettblog.eu/blog/2013/09/20/using-grunt-connect-proxy/

Http://www.himysql.com/2014/07/29/grunt-connect-proxy-configure/

Http://www.ngnice.com/posts/76c4bd0f7a4cdc

Grunt-connect-proxy Resolving cross-domain issues during development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.