为什么80%的码农都做不了架构师?>>>   

#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, it is ideally designated as a 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.

<pre> / /client /server </pre>

The client is an AngularJS based application, I reused the AngularJS Seed sandbox, and copy the app folder in the former Grails application.

The server is nearly no difference from the original Grails application, 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).

https://spring.io/understanding/cors

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. Use the following command to install the templates for this projects.

<pre> grails install-templates </pre>

Open the web.xml file under the src/templates/war folder.

Add the configuration of CORS filter.

<pre> &lt;filter> &lt;filter-name>CORS Filter&lt;/filter-name> &lt;filter-class>org.ebaysf.web.cors.CORSFilter&lt;/filter-class> &lt;init-param> &lt;description>A comma separated list of allowed origins. Note: An '*' cannot be used for an allowed origin when using credentials.&lt;/description> &lt;param-name>cors.allowed.origins&lt;/param-name> &lt;param-value>http://localhost:8000, http://localhost:8080&lt;/param-value> &lt;/init-param> &lt;init-param> &lt;description>A comma separated list of HTTP verbs, using which a CORS request can be made.&lt;/description> &lt;param-name>cors.allowed.methods&lt;/param-name> &lt;param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE&lt;/param-value> &lt;/init-param> &lt;init-param> &lt;description>A comma separated list of allowed headers when making a non simple CORS request.&lt;/description> &lt;param-name>cors.allowed.headers&lt;/param-name> &lt;param-value>Content-Type,X-Requested-With,Authorization,Origin,Access-Control-Request-Method,Access-Control-Request-Headers&lt;/param-value> &lt;/init-param> &lt;init-param> &lt;description>A comma separated list non-standard response headers that will be exposed to XHR2 object.&lt;/description> &lt;param-name>cors.exposed.headers&lt;/param-name> &lt;param-value>&lt;/param-value> &lt;/init-param> &lt;init-param> &lt;description>A flag that suggests if CORS is supported with cookies&lt;/description> &lt;param-name>cors.support.credentials&lt;/param-name> &lt;param-value>true&lt;/param-value> &lt;/init-param> &lt;init-param> &lt;description>A flag to control logging&lt;/description> &lt;param-name>cors.logging.enabled&lt;/param-name> &lt;param-value>true&lt;/param-value> &lt;/init-param> &lt;init-param> &lt;description>Indicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache.&lt;/description> &lt;param-name>cors.preflight.maxage&lt;/param-name> &lt;param-value>10&lt;/param-value> &lt;/init-param> &lt;/filter> &lt;filter-mapping> &lt;filter-name>CORS Filter&lt;/filter-name> &lt;url-pattern>/*&lt;/url-pattern> &lt;/filter-mapping> </pre>

Run the server project as a Grails application from command line or IDE.

And then run the client project as a NodeJS application.

<pre> node scripts\web-server.js </pre>

Navigate to http://localhost:8000/app/index.html#/.

##Sample codes

The code is hosted on https://github.com/hantsy/angularjs-grails-sample/.

转载于:https://my.oschina.net/hantsy/blog/185667

Create a restful app with AngularJS/Grails(4)相关推荐

  1. Create a restful application with AngularJS and CakePHP (I)

    为什么80%的码农都做不了架构师?>>>    #Create a restful application with AngularJS and CakePHP (I) #Prepa ...

  2. 如何用 Siesta 编写 RESTful app

    原文:How to make a RESTful app with Siesta 作者:Sanket Firodiya 译者:kmyhy 通过网络获取数据是移动应用程序中最常见的一种任务.因此,像 a ...

  3. 第二讲 html5框架+Crosswalk打包app 以及 Angularjs 基础(初步认识了解Angularjs)

    第二讲 html5框架+Crosswalk打包app 以及 Angularjs 基 础(初步认识了解Angularjs) 学习要点: 1. html5框架+Crosswalk打包app 2. 什么是a ...

  4. 第二讲 html5 框架+Crosswalk 打包 app 以及 Angularjs 基础

     第二讲 html5框架+Crosswalk 打包 app 以及 Angularjs 基础(初步认识了解 Angularjs)   学习要点: 1. html5 框架+Crosswalk 打包 a ...

  5. 利用 Create React Native App 快速创建 React Native 应用

    React Native App简介 打开React Native官方文档你会发现,在Getting Started章节下新添加一个Quick Start Tab页.Quick Start是在v0.4 ...

  6. Azure Create a function app Review+Create栏没有create

    [问题]如图create不了 [解决] Basic一栏,subscription换一个有权限的,resource group新建一个

  7. 如何使用Create React App DevOps自动化工作中所有无聊的部分

    by James Y Rauhut 詹姆士·鲁豪(James Y Rauhut) 如何使用Create React App DevOps自动化工作中所有无聊的部分 (How I automate al ...

  8. Building Huuuuuge Apps with AngularJS

    [2013.01.07] The AngularJS documentation is great for getting started and for looking up specific AP ...

  9. backbone, AngularJS, EmberJS 简单比较

    1 backbone. 对于初学者来说,我非常建议首先学习backbone 而不是jQuery.因为jQuery提供的功能是操作DOM和Ajax数据传输. 而Backbone的功能和目的仅仅是提供一个 ...

最新文章

  1. SpringBoot(二):设置springboot同一接口程序启动入口
  2. “禁止大数据杀熟”拟入法!个性化推荐功能也应提供拒绝选项
  3. Crawler:关于爬虫的简介、安装、使用方法之详细攻略
  4. Java 算法 素数对猜想
  5. mysql.5.7 linux_Mysql5.7 Linux安装教程
  6. js方法的使用(z)
  7. CSS3 transform-style 属性
  8. solr的索引库配置
  9. Navicat批量修改数据库某一属性初始值
  10. Switch新机发布引全球吐槽,老任给牙膏厂上了一课,这波是等等党输了
  11. iptables的三表五链
  12. BootStrap Table:列参数
  13. Word2019版_去掉文档中的回车符
  14. 转载:80端口、443端口、8080端口、8000端口的区别
  15. unity 2d地图tilemap滚轮缩放出现缝隙解决办法
  16. web4.0之万能密码登录
  17. 2016年全国高中数学联赛加试T1解答
  18. linux mysql编译参数,Mysql 编译参数详解
  19. 基于css的表单模板
  20. 网络营销几种简单的反复(下)

热门文章

  1. linux上使用strace查看C语言级别的php源码【一种方法】
  2. 用C语言写PHP扩展
  3. “分布式哈希”和“一致性哈希”的概念与算法实现
  4. 关于SSL配置的报告
  5. C#程序设计语言Version2.0简介
  6. 图像处理和图像识别中常用的OpenCV函数
  7. 【Qt】Qt样式表总结(四):CSS盒子模型
  8. android wear无法启用,android-wear – 无法创建Android虚拟设备,“没有为此目标安装系统映像”...
  9. Java学习总结:40(国际化)
  10. ios采用什么技术_在不锈钢技术成熟的今天,为什么汽车不采用呢?不仅仅是价格问题...