Nodejs in Visual Studio Code 03.学习Express
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的更多相关文章
- crossplatform---Nodejs in Visual Studio Code 03.学习Express
1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...
- Nodejs in Visual Studio Code 07.学习Oracle
1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...
- Nodejs in Visual Studio Code 02.学习Nodejs
1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...
- Nodejs in Visual Studio Code 10.IISNode
1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...
- 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 ...
- Nodejs in Visual Studio Code 11.前端工程优化
1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...
- Nodejs in Visual Studio Code 04.Swig模版
1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...
- Nodejs in Visual Studio Code 01.简单介绍Nodejs
1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...
- crossplatform---Nodejs in Visual Studio Code 07.学习Oracle
1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...
随机推荐
- tomcat 8.0 安装
tomcat 安装算是非常简单的, 因自己使用ubuntu,所以以下环境均为 ubuntu 操作系统下 tomcat 官方简介 tomcat 各版本下载 这里我使用二进制版本(binary distr ...
- .net(全局文件,错误页,静态页,IIS配置及防黑)
添加全局应用程序类. <%@ Application Language="C#" %> <script runat="server"> ...
- 循序渐近学docker---笔记
1.安装docker 环境:ubuntu 16.04 sudo apt-get install docker.io root@ld-Lenovo-G470:~# docker -vDocker ver ...
- ASP.NET面试
1.net中读写数据库需要用到那些类?他们的作用都是什么?答:DataSet:数据存储器.DataCommand:执行语句命令.DataAdapter:数据的集合,用语填充.2.介绍一下什么是Code ...
- jquer “$” 扩展(笔记)
/** * Created by shanruo on 16-03-10. */ (function ( $ ){ $.extend ({ /* * 根据参数名获取参数 @name String 参数 ...
- 关于svn获取获取文件时 Unable to connect to a repository at URL"https://..."执行上下文错误:参数错误
错误提示: 下面的六种解决方案都未能解决: 1.不提示输入用户名和密码,不管重装多少次都一样. 2.TortoiseSVN的setting->Saved Data->Authenticat ...
- VS2008试用版到期解决办法
在Windows 7下,VS2008试用版无法正常升级到正式版.原因是维护页面的注册码输入框和升级按钮被隐藏.通过本补丁,可让注册码输入框和升级按钮恢复正常显示 点击此处下载补丁 1.安装完90天试用 ...
- Linux 抓取网站命令
wget -m -e robots=off -U "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/200 ...
- cxf客户端代码wsdlLocation设置相对路径
利用工生成的cxf客户端代码,wsdlLocation都是绝对路径,为了便于项目更加灵活管理,我们可以将该路径设置为相对路径: 1.下面图片是我的项目路径图片及wsdl地址存放路径: 2.下面图片是我 ...
- TalkingDataGame SDK在android Lua上的使用
千呼万唤使出来...终于开始更新lua版本的内容了,之前一直有这方面的计划,由于公司业务比较多,一直比较忙-见谅.. 费话不多说,直接上内容.. 整体来讲,先是先建议看一下之前关于cocos2dx上的 ...