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. [CSS] Control Image Aspect Ratio Using CSS

    Resize images and videos to fill their parent and maintain their aspect ratio with pure CSS. The new ...

  2. 看朋友日志发现的一个ios下block相关的内存管理问题,非常奇怪,请大家帮忙一起来回答!

    http://blog.csdn.net/fengsh998/article/details/38090205 这篇文章以下是我的回复.相同的代码仅仅是把变量的定义从局部变量改为类的成员变量就发现了非 ...

  3. Kinect 骨骼映射---Let me dance for U!

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/45583443 作者:ca ...

  4. DI:依赖注入详解

    DI(依赖注入) 依赖注入的理解: 一般写程序的时候service层都需要用到dao层,所以一般都是在service层里面new  dao ,而现在利用依赖注入的方式,直接把dao给了service层 ...

  5. 【t035】收入计划

    Time Limit: 1 second Memory Limit: 32 MB [问题描述] 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天( ...

  6. Django之模板过滤器

    Django 模板过滤器也是我们在以后基于 Django 网站开发过程中会经常遇到的,如显示格式的转换.判断处理等.以下是 Django 过滤器列表,希望对为大家的开发带来一些方便. 一.形式:小写 ...

  7. Android 离线缓存的高速实现

    离线缓存是指在有网络的状态下将从server获取的网络数据.如Json 数据缓存到本地,在断网的状态下启动APP时读取本地缓存数据显示在界面上,经常使用的APP(网易新闻.知乎等等)都是支持离线缓存的 ...

  8. 自旋锁解决StackOverflowError案例

    本节笔者分享一个在实际工作中遇到的栈内存溢出(StackOverflowError)问题,以及其解决方案. 问题介绍:笔者负责的一个Java Web项目在启动的时候,需要有一些初始化操作,而接下来的代 ...

  9. 《学习opencv》笔记——关于一些画图的函数

    画图函数 (1)直线cvLine函数 其结构 void cvLine(//画直线 CvArr* array,//画布图像 CvPoint pt1,//起始点 CvPoint pt2,//终点 CvSc ...

  10. 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现

    原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...