Standalone AngularJS application
In the real world applications, it is usually required to integrate the third party APIs into the project. Most of case, the third party service is provided as flexible REST APIs for developers.
Imagine the REST API in this sample could sever other applications, such as a mobile app or other website in future, ideally it should be designated as an API centric application.
The AngularJS codes can be moved to a standalone application, and consume the REST web service remotely, like other applications.
Split the original codes into two projects.
/ /client /server
The client is an AngularJS based application. The codes are based on AngularJS Seed sandbox. I copied the app folder in the former Grails application.
The server is nearly no difference from the original Grails part, but the app folder in the web-app folder is moved to client. Additionally, you have to configure CORS in the server side.
Configure CORS
There is a good post from the Spring community to help you understand CORS(Cross-Origin Resource Sharing).
The post provides a solution to resolve the limitation of cross domain resource accessing.
But I would like use a CORS filter from Ebay, I have used it in projects and it has some configuration options.
By default, the Grails application does not includes a web.xml file, it is generated from the default templates at runtime. Use the following command to install the templates for this projects.
grails install-templates
Open the web.xml file under the src/templates/war folder.
Configure CORS filter in the web.xml.
<filter> <filter-name>CORS Filter</filter-name> <filter-class>org.ebaysf.web.cors.CORSFilter</filter-class> <init-param> <description>A comma separated list of allowed origins. Note: An '*' cannot be used for an allowed origin when using credentials.</description> <param-name>cors.allowed.origins</param-name> <param-value>http://localhost:8000, http://localhost:8080</param-value> </init-param> <init-param> <description>A comma separated list of HTTP verbs, using which a CORS request can be made.</description> <param-name>cors.allowed.methods</param-name> <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value> </init-param> <init-param> <description>A comma separated list of allowed headers when making a non simple CORS request.</description> <param-name>cors.allowed.headers</param-name> <param-value>Content-Type,X-Requested-With,Authorization,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value> </init-param> <init-param> <description>A comma separated list non-standard response headers that will be exposed to XHR2 object.</description> <param-name>cors.exposed.headers</param-name> <param-value></param-value> </init-param> <init-param> <description>A flag that suggests if CORS is supported with cookies</description> <param-name>cors.support.credentials</param-name> <param-value>true</param-value> </init-param> <init-param> <description>A flag to control logging</description> <param-name>cors.logging.enabled</param-name> <param-value>true</param-value> </init-param> <init-param> <description>Indicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache.</description> <param-name>cors.preflight.maxage</param-name> <param-value>10</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Run the server project as a Grails application from command line or IDE. Now it works as a REST API producer and severs client applications.
And then run the client project as a NodeJS application.
node scripts\web-server.js
Navigate to http://localhost:8000/app/index.html#/.
Sample codes
The code is hosted on https://github.com/hantsy/angularjs-grails-sample/.
评论