1.开始

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

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

2.ExpressRest

打开目录08_ExpressREST

app.js

var express = require('express');
var app = express();

//捕获GET方法,并处理返回一个Json,比C#写Json简单多了啊
app.get('/', function (req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});

//侦听8080端口
app.listen(process.env.PORT || 8080);

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

打开CMD让项目运行起来

$ cd 08_EXPRESSREST
$ node app

3.AdvancedRESTAPI

打开目录12_AdvancedRESTAPI

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

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

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

//初始化一个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.

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;

  

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

  1. crossplatform---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 10.IISNode

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

  5. 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 ...

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

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

  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. 百度地图api之如何自定义标注图标

    在百度地图api中,默认的地图图标是一个红色的椭圆形.但是在项目中常常要求我们建立自己的图标,类似于我的这个 操作很简单,分如下几步进行 步骤一:先ps一个图标,大小要合适,如果要背景透明的,记得保存 ...

  2. 试答卓同学的 iOS 面试题

    卓同学昨天写了一篇文章<4道过滤菜鸟的iOS面试题>.我手痒决定默写一个参考答案.后来发现不认真回答被大家喷成狗,所以决定积极改造,重新做人.下面就是修编之后的答案. 1. struct和 ...

  3. Codeforces 526D - Om Nom and Necklace 【KMP】

    ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...

  4. Android中SQLite使用

    现在的主流移动设备像Android.iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储我们大量的数据,所以我们就需要掌握移动设备上 ...

  5. Cookie的读写

    记住怎么写就可以了,不要问我为什么=_= 设置值的页面:context.Response.SetCookie(new HttpCookie("username",username) ...

  6. WEB前端开发规范文档(转)

    http://codeguide.bootcss.com/  编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范上面的文档 再结合下面的规范: 无论是从技术角度还是开发视角,对于web前端开发 ...

  7. 鼠标滑动判断与y轴的距离

     $(window).scroll(function(){                        var y = window.scrollY;//754到达                  ...

  8. 【转】JAVA的StringBuffer类

    [转]JAVA的StringBuffer类    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBu ...

  9. lucene查询排序结果原理总结

    参考文章 Lucene3.0结果排序原理+操作+示例 Lucene的排序算法 一句话总结lucene排序算法是什么样的 关键几个概念 参考文档: http://lucene.apache.org/co ...

  10. Excel 2007中的新文件格式

    *.xlsx:基于XML文件格式的Excel 2007工作簿缺省格式 *.xlsm:基于XML且启用宏的Excel 2007工作簿 *.xltx:Excel2007模板格式 *.xltm:Excel ...