做了一个简单的演示样例,目的是记录环境配置以及这套框架的结构流程。

1.配置环境

默认nodejs已安装。

安装下面模块:express(nodejs框架),grunt(javascript task runner),grunt-contrib-watch(grunt live load插件),grunt-express-server(grunt启动express服务端插件)。

命令行中进入程序文件夹,依次输入下面命令:

npm install express 回车

npm install grunt  回车

npm install grunt-contrib-watch 回车

npm install grunt-express-server 回车

成功安装后,能够在程序目录中的node_modules目录里看到对应的模块目录:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnlfZm9yZXZlcg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

2.配置grunt 任务

打开程序文件夹下的Gruntfile.js文件。注冊express和watch任务。

express任务启动express服务器而且执行server.js文件。watch任务监视express任务以及live reload。代码例如以下:

module.exports = function(grunt) {
//config project
grunt.initConfig({
watch: {
options: {
livereload: true,
},
express: {
files: [ 'server.js' ],
options: {
spawn: false
}
}
},
//start express server and run server.js
express: {
options: {
// Override defaults here
},
dev: {
options: {
script: 'server.js'
}
}
}
});
//enable plugins
grunt.loadNpmTasks('grunt-express-server');
grunt.loadNpmTasks('grunt-contrib-watch');
//register task
grunt.registerTask('default', ['express','watch']);
};

3. 主要文件

serve_data.js。server.js和index.html都在程序文件夹下。

index.html用angularjs resource向server上的‘/data’路径发起http请求。

在server.js中定义了路径‘/data’的行为是返回通过serve_data.js文件里的getData()方法获取的data变量。

index.html 的resource收到返回的data后,通过数据绑定{{data}}将其显示在页面上。

三个文件详细代码及凝视例如以下:

index.html:

<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script
src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-resource.min.js"></script>
</head>
<body ng-app="myApp" ng-controller=MainCtrl>
<div>{{data}}</div>
<script>
var myApp = angular.module('myApp', [ 'ngResource' ]);
//define app factory
myApp.factory('DataFac', function($resource) {
//request data from route '/data'
return $resource('data');
}); //define app controller
myApp.controller('MainCtrl', function($scope, DataFac) {
DataFac.get(function(response) {
$scope.data = response;
})
});
</script>
</body>
</html>

server.js:

//use express
var express = require('express');
var app = express(); //require file serve_data.js to use its exported modules
var instance=require('./serve_data.js')
var data=instance.getData(); //define route
app.get('/data',function(req,res){
res.send(data);
}); //serve static index.html as default
app.use(express.static(__dirname + '/')); //bind and listen for connections on the given host and port
app.listen(9001,function(){
console.log('Server listening on',9001)
})

serve_data.js:

//export funtion getData
module.exports={
getData:function(){
//data can be fetched from a database or a file and so on. Here for simplicity,provide json data directly
var data={"widget": {
"debug": "on",
"window": {
"title": "Sample Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/test.png",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center"
}
}} ; return data;
}
}

4.执行程序

在命令行中进入程序文件夹,输入grunt执行grunt任务。打开浏览器进入http://localhost:9001/  ,得到下面结果:

最新文章

  1. Linux新手扫盲(转载)
  2. java.lang.NoSuchFieldError 异常原因
  3. Linux主机规划
  4. php获取post参数的几种方式 RPC 规定接收取值方式 $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;];
  5. actionlib的身世之谜
  6. [zt]OpenCV2.1.0的安装
  7. Linux下守护进程初探
  8. http和数据库sql分析与窃听技术
  9. 从tcp原理角度理解Broken pipe和Connection reset by peer的区别
  10. 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框
  11. ListView的小知识
  12. 转:Http头介绍:Expires,Cache-Control,Last-Modified,ETag
  13. JavaScript编程:java事件模型
  14. Ruby 一些经常使用的细节
  15. laravel提示Mcrypt PHP extension required
  16. [2013-02-22]info入门FAQ
  17. Android线程间通信机制——深入理解 Looper、Handler、Message
  18. SpringCloud的微服务网关:zuul(实践)
  19. 时区切换导致quartz定时任务没有触发问题
  20. linux 环境变量 转

热门文章

  1. 【习题 7-5 UVA-690】Pipeline Scheduling
  2. 《从零開始学Swift》学习笔记(Day 59)——代码排版
  3. ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第二篇:第一个页面
  4. 将一个字符串当做一个方法名或对象的key
  5. Java对ad操作
  6. Android 软键盘弹出,界面整体上移的问题
  7. synchronized和ReentrantLock区别
  8. 利用Attribute实现Aop
  9. 2、Python基本数据类型
  10. testng并发测试与测试并发