1.开始

下载源码:https://github.com/sayar/NodeMVA

Express组件:npm install express -g(全局安装)

2.ExpressRest

打开目录08_ExpressREST

app.js

1
2
3
4
5
6
7
8
var express = require('express');
var app = express();
<br>//捕获GET方法,并处理返回一个Json,比C#写Json简单多了啊
app.get('/'function (req, res) {
    res.json({ message: 'hooray! welcome to our api!' });
});
<br>//侦听8080端口
app.listen(process.env.PORT || 8080);

现在项目都用html/js了,写个node几行代码搞定json restful,还带web服务器,感觉神清气爽啊!

打开CMD让项目运行起来

1
2
cd 08_EXPRESSREST
$ node app

3.AdvancedRESTAPI

打开目录12_AdvancedRESTAPI

app.js:初始化Express环境注册路由

routes/api.js :rest api的具体实现代码

app.js中,注册路由代码十分简单

1
2
3
4
5
6
7
8
//初始化一个api.js实例
var api = require('./routes/api');
 
//初始化一个express.js实例
var app = express();
 
//将指定url路由处理程序指向api.js文件
app.use('/api', api);

routes/api.js中对每个api后的url按GET\PUT\POST\DELETE分别处理

Resource

GET

PUT

POST

DELETE

Collection URI, such as http://api.example.com/v1/dogs/

List all the dogs

Replace all the dogs with a new collection of dogs.

Create a new dog in the collection.

Delete the entire dog collection.

Element URI, such as http://api.example.com/v1/dog/1

Get a specific dog.

Replace a dog in the collection with another dog.

Not used.

Delete the dog from the collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var express = require('express');
var router = express.Router();
 
var dogs = [
  {
    "dog_id""0",
    "dog_name""Ginger"
  },
  {
    "dog_id""1",
    "dog_name""Ruby"
  },
  {
    "dog_id""2",
    "dog_name""Buddy"
  }
];
 
/* GET all dogs */
router.get('/dogs/'function(req, res, next) {
  res.json(dogs);
});
 
/* PUT replace all dogs */
router.put('/dogs/'function(req, res, next) {
  console.log(req.body);
  dogs = req.body;
  res.json({"STATUS""200 OK"});
});
 
/* POST create a new dog */
router.post('/dogs/'function(req, res, next) {
  dogs.push(req.body)
  res.json({"STATUS""200 OK"});
});
 
/* DELETE delete the entire dog collection */
router.delete('/dogs/'function(req, res, next) {
  dogs = [];
  res.json({"STATUS""200 OK"});
});
 
 
/* GET a specific dog */
router.get('/dogs/:id'function(req, res, next) {
  var i = 0;
  var dog = null;
  for(i = 0; i != dogs.length; i++){
    if(dogs[i].dog_id == req.params.id){
      dog = dogs[i];
      break;
    }
  }
  dog !== null ? res.json(dog) : res.json({"STATUS""404 NOT FOUND"})
});
 
/* PUT replace a specific dog with another dog */
router.put('/dogs/:id'function(req, res, next) {
  var i = 0;
  var dog = null;
  for(i = 0; i != dogs.length; i++){
    if(dogs[i].dog_id == req.params.id){
      dog = dogs[i];
      break;
    }
  }
  if(dog !== null){
    dog.dog_name = req.body['dog_name']
    res.json({"STATUS""200 OK"});
  else {
    res.json({"STATUS""404 NOT FOUND"});
  }
});
 
/* DELETE a specific dog from the collection */
router.delete('/dogs/:id'function(req, res, next) {
  var i = 0;
  for(i = 0; i != dogs.length; i++){
    if(dogs[i].dog_id == req.params.id){
      dogs.splice(i, 1);
      return res.json({"STATUS""200 OK"});
    }
  }
  return res.json({"STATUS""404 NOT FOUND"});
});
 
 
module.exports = router;

http://www.cnblogs.com/mengkzhaoyun/p/5355796.html

crossplatform---Nodejs in Visual Studio Code 03.学习Express的更多相关文章

  1. Nodejs in Visual Studio Code 03.学习Express

    1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...

  2. Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

  3. Nodejs in Visual Studio Code 02.学习Nodejs

    1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...

  4. Nodejs in Visual Studio Code 14.IISNode与IIS7.x

    1.开始 部署IISNode环境请参考:Nodejs in Visual Studio Code 08.IIS 部署Nodejs程序请参考:Nodejs in Visual Studio Code 1 ...

  5. Nodejs in Visual Studio Code 11.前端工程优化

    1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...

  6. Nodejs in Visual Studio Code 10.IISNode

    1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...

  7. Nodejs in Visual Studio Code 04.Swig模版

    1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...

  8. Nodejs in Visual Studio Code 01.简单介绍Nodejs

    1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...

  9. crossplatform---Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

随机推荐

  1. 【Codeforces Round #185 (Div. 2) B】Archer

    [链接] 链接 [题意] 在这里输入题意 [题解] 概率水题. 枚举它是第几轮成功的. 直到满足精度就好 [错的次数] 1 [反思] long double最让人安心. [代码] #include & ...

  2. js页面载入特效如何实现

    js页面载入特效如何实现 一.总结 一句话总结:可以加选择器(里面的字符串)作为参数,这样函数就可以针对不同选择器,就很棒了. 1.特效的原理是什么? 都是通过标签的位置和样式来实现特效的. 二.js ...

  3. 数值优化(Numerical Optimization)学习系列-文件夹

    概述 数值优化对于最优化问题提供了一种迭代算法思路,通过迭代逐渐接近最优解,分别对无约束最优化问题和带约束最优化问题进行求解. 该系列教程能够參考的资料有 1. <Numerical Optim ...

  4. Js数组排序函数:sort()

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78979521 js原生的 sort() 排序函数使用起来很方便 语法:arrayObj ...

  5. [Angular] Create dynamic content with <tempalte>

    To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry c ...

  6. 社会化登录分享-Android SDK的二次封装和使用

    本系列文章将第三方的登录分享功能进行二次封装,统一接口调用,简化了接不同平台登录分享的步骤. 0 系列文章 系列一 Android SDK的二次封装和使用 系列二 源码解析 系列三 微信SDK接入 系 ...

  7. MapReduce 编程 系列九 Reducer数目

    本篇介绍怎样控制reduce的数目.前面观察结果文件,都会发现通常是以part-r-00000 形式出现多个文件,事实上这个reducer的数目有关系.reducer数目多,结果文件数目就多. 在初始 ...

  8. 学习鸟哥的Linux私房菜笔记(12)——系统监视2

    四.控制进程 kill :语法 kill [-signal] PID     向进程传送一个特定的讯号,默认为15(终结) kill -l  :列出所有可以由kill传递的讯号 1 :重启进程 2 : ...

  9. [GeekBand ] 利用 pass by reference -to -const 编写高效规范的 c++代码

    本文参考资料 :  GeekBand 侯捷老师,学习笔记 Effective C ++ 侯捷译 条款20 开发环境采用:VS2013版本 首先:分析值传递的缺点 (一) class Person{ p ...

  10. Python 工具类与工具函数 —— pair

    def pair(lis): n = len(lis) for i in range(n): for j in range(i+1, n): yield lis[i], lis[j] 这样在调用端,访 ...