码迷,mamicode.com
首页 > 其他好文 > 详细

ng2 与 node expree get 与post 数据

时间:2017-08-04 22:53:03      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:name   string   -o   turn   headers   成功   .post   script   asc   

最近在学习ng2,node.js等。 redis 数据库

实现angular 与 node 后台数据交互

搭建的环节为

1、ng2 本地服务 http://127.0.0.1:4200

2、node.js express 后台服务 为:http://127.0.0.3000 (数据库使用redis,)

 

在ng2 通过 http请求调用 node后台服务数据 (会遇到同源策略问题 )

在node服务的 app.js 中 把同源策略问题解决

app.all(‘*‘, function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://127.0.0.1:4200");
    res.header("Access-Control-Allow-Headers",  "Content-Type");// "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",‘ 3.2.1‘)
    res.header("Content-Type",  "application/json;charset=utf-8" );// "application/json;charset=utf-8");  //text/html;charset=utf-8
    next();
});

 ng2 请求 get 请求

   getUsers():Observable < User[] > {
        return this.http.get(this.userListUrl)
        		.map(this.extractData)
        		.catch(this.handleError);
              // .catch(this.handleError);
    }

 node接受处理 ,(redis是非堵塞方式,在后台改成同步方式处理数据)

 1 router.get(‘/getUserList‘, function(req, res, next) {
 2    // res.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
 3     co(function *() {
 4         try{
 5             var list = [];
 6           
 7             /* if(sn=="")
 8             {*/
 9             var keys=yield findKey2("users*");
10             console.log(keys);
11             for(var j in keys)
12             {
13                 console.log(keys[j]);
14                var getValues= yield hGetAll2(keys[j]);
15                 list.push(getValues);
16                 console.log(getValues.name);
17             }
18            // res.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
19             res.json({"data": list});
20             //res.render(‘start/devcontrol‘, { title: ‘测试redis‘ ,‘user‘:msg });
21         }
22         catch (e) {
23             console.log(e)
24         }
25     }),(function(err,d){console.log(err)});
26 });

2、post    数据 按照{ ‘Content-Type‘: ‘application/json‘ }

ng2 请求 提交{id:id, name:name } 对象

1   create(id:number,name: string): Observable<User> {
2         let headers = new Headers({ ‘Content-Type‘: ‘application/json‘ });
3         let options = new RequestOptions({ headers: headers });
4       return this.http.post(this.userSaveUrl,  JSON.stringify({id:id, name:name }) ,options)
5                             .map(this.extractData)
6                             .catch(this.handleError)
7     
8       }

 

node 接受处理   bodyParms=req.body; 等保存成功之后 再返回到ng2

 1 router.post(‘/saveUser‘, function(req, res, next) {
 2     co(function *() {
 3         try{
 4             var list = [],newUser={};
 5 
 6             var bodyParms=req.body;
 7             console.log(req.body);
 8             console.log(req.query);
 9             console.log(req.params);
10                    var key="users"+bodyParms.id;
11             
12             newUser.id=bodyParms.id;
13             newUser.name=bodyParms.name;
14               console.log(newUser);
15             for(var j in newUser){
16                 var x = yield hsetkey2(key,j,newUser[j]);
17             }
18             var getValues= yield hGetAll2(key);
19             res.json({"data": getValues});
20         
21         }
22         catch (e) {
23             console.log(e)
24         }
25     }),(function(err,d){console.log(err)});
26 });

 

继续学习中.....

 

ng2 与 node expree get 与post 数据

标签:name   string   -o   turn   headers   成功   .post   script   asc   

原文地址:http://www.cnblogs.com/yucm/p/7287096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!