基于Express.js框架,使用RESTful Api

关于项目中的 service.js
我将'gnomeontherun'演示项目中所使用的配置代码黏贴上来.
来自:[url]https://github.com/ionic-in-action/chapter3[/url]



//注意,如果你没有配置依赖相关框架将会报错
//你可以手动单独安装这些框架

//npm install body-parser
//npm install express --save

//express是什么? Look This →_→ [url]http://www.expressjs.com.cn/[/url]
var express = require('express');

//这是nodeJs的文件系统模块,是一个简单包装的标准 POSIX 文件 I/O 操作方法集.
//这里之所以要使用我看他想读取一些演示数据 'data.json'
var fs = require('fs');

//bodyParser用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理.
var bodyParser = require('body-parser');

var app = express();

app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.set('Content-Type', 'application/json');
next();
});

//这是一个app的http请求
app.get('/notes', function (req, res) {
// Open the existing notes file
fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

res.status(200).send(data);

});
})

// Update a note
app.put('/notes/:id', function (req, res) {
// Open the existing notes file
fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

// Try to parse the JSON or return
try {
data = JSON.parse(data);
} catch (e) {
res.status(500).end();
return console.log(e);
}

// Add body item to notes array
data.forEach(function (note, index) {
if (note.id == req.params.id) {
data[index] = req.body;
}
});

// Write file back to server
fs.writeFile(__dirname + '/data/notes.json', JSON.stringify(data), function (err) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

// No errors, everything is done so return new data
res.status(200).send(data);
});
});
});

// Create a new note
app.post('/notes', function (req, res) {
// Open the existing notes file
fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

// Try to parse the JSON or return
try {
data = JSON.parse(data);
} catch (e) {
res.status(500).end();
return console.log(e);
}

// Add body item to notes array
data.push(req.body);

// Write file back to server
fs.writeFile(__dirname + '/data/notes.json', JSON.stringify(data), function (err) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

// No errors, everything is done so return new data
res.status(201).send(data);
});
});
});

// Delete a note
app.delete('/notes/:id', function (req, res) {
// Open the existing notes file
fs.readFile(__dirname + '/data/notes.json', 'utf8', function (err, data) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

// Try to parse the JSON or return
try {
data = JSON.parse(data);
} catch (e) {
res.status(500).end();
return console.log(e);
}

// Add body item to notes array
var index = -1;
data.forEach(function (note, i) {
if (note.id == req.params.id) {
index = i;
}
});

// If we found an item by that id, remove it
if (index >= 0) {
data.splice(index, 1);
}

// Write file back to server
fs.writeFile(__dirname + '/data/notes.json', JSON.stringify(data), function (err) {

// If we get an error, log it and return
if (err) {
res.status(500).end();
return console.log(err);
}

// No errors, everything is done so return
res.status(204).end();
});
});
});

//App的web端口监听在这里配置.
app.listen(3000, function () {
console.log('Server started. Open http://localhost:3000 in your browser.');
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值