Restify编写RestfulApi框架结构设计
标签(空格分隔): Node.js
[TOC]
文件目录结构
使用了MVC中的Model和Controller
app.js入口文件
var restify = require('restify');
var config = require('./config');
var app = restify.createServer({name: 'REST-api'});
app.use(restify.fullResponse());
app.use(restify.bodyParser());
app.use(restify.queryParser());
app.listen(config.port, function(){
console.log('server listening on port number', config.port);
});
var routes = require('./routes')(app);
config.json配置文件
{
"dbPath": "mongodb://localhost/rest-api",
"port": 8000
}
db.js数据库文件
var mongoose = require('mongoose');
var config = require('./config');
mongoose.connect(config.dbPath);
var db = mongoose.connection;
db.on('error', function() {
console.log('error occured from db');
});
db.once('open', function dbOpen(){
console.log('successfully opened the db');
});
exports.mongoose = mongoose;
routes.js路由分发文件
module.exports = function(app) {
var student = require('./controllers/studentController');
var article = require('./controllers/articleController');
var comment = require('./controllers/commentController');
app.get('/', function(req, res, next){
return res.send("WELCOME TO REST API");
});
app.get('/student', student.getStudents);
app.post('/student', student.createStudent);
app.get('/student/:id', student.getStudent);
app.del('/student/:id', student.deleteStudent);
app.put('/student/:id', student.updateStudent);
app.get('/articles', article.viewArticles);
app.post('/articles', article.createArticle);
app.get('/articles/:id', article.getArticle);
app.del('/articles/:id', article.deleteArticle);
app.put('/articles/:id', article.updateArticle);
app.post("/articles/:id/comments", comment.createComment);
app.get("/articles/:id/comments", comment.viewComments);
app.get("/articles/:id/comments/:commentId", comment.viewComment);
app.put("/articles/:id/comments/:commentId", comment.updateComment);
app.del("/articles/:id/comments/:commentId", comment.deleteComment);
};
Article.js文章模型文件
module.exports = (function studentSchema() {
var mongoose = require('../db').mongoose;
var Comment = require('./Comment');
var schema = {
title: String,
slug: String,
content: String,
author: {
type: String,
ref: "User"
},
comments: [Comment.schema]
}
var collectionName = 'article';
var articleSchema = mongoose.Schema(schema);
var Article = mongoose.model(collectionName, articleSchema);
return Article;
})();
articleController.js文章控制器文件
function articleController() {
var mongoose = require('mongoose');
var Article = require('../models/Article');
ObjectId = mongoose.Types.ObjectId;
this.createArticle = function(req, res, next){
var slug = require('limax');
var title = req.body.title;
var slug = slug(title);
var i = 1;
while (loopTrhoughSlugs(slug) == true) {
slug = slug + "-" + i;
i++;
}
var content = req.body.content;
var articleModel = new Article({title: title, slug: slug, content: content});
articleModel.save(function(err, article){
if(err){
res.status(500);
res.json({
type: false,
data: "Error occured: " + err
})
} else {
res.json({
type: true,
data: article
})
}
})
}
function loopTrhoughSlugs(slug){
Article.find({"slug": slug}, function(err, article){
if(err){
res.status(500);
res.json({
type: false,
data: "Error occured: " + err
})
} else {
console.log(article);
if(article){
return true;
} else {
return false;
}
}
})
}
this.viewArticles = function(req, res, next){
Article.find({}, function(err, articles){
if(err){
res.status(500);
res.json({
type: false,
data: "Error occured: " + err
})
} else {
res.json({
type: true,
data: articles
})
}
})
}
this.getArticle = function(req, res, next){
Article.findById(new ObjectId(req.params.id), function(err, article){
if(err){
res.status(500);
res.json({
type: false,
data: "Error occured: " + err
})
} else {
if(article){
res.json({
type: true,
data: article
})
} else {
res.json({
type: false,
data: "Article: " + req.params.id + " not found"
})
}
}
})
}
this.updateArticle = function(req, res, next){
var title = req.params.title;
var slug = req.params.slug;
var content = req.params.content;
Article.findByIdAndUpdate(new ObjectId(req.params.id), {title: title, slug: slug, content: content}, function(err, article){
if(err){
res.status(500);
res.json({
type: false,
data: "Error occured: " + err
})
} else {
if(article){
res.json({
type: true,
data: article
})
} else {
res.json({
type: false,
data: "Article: " + req.params.id + " not found"
})
}
}
})
}
this.deleteArticle = function(req, res, next){
Article.findByIdAndRemove(new ObjectId(req.params.id), function(err, article){
if(err){
res.status(500);
res.json({
type: false,
data: "Error occured: " + err
})
} else {
res.json({
type: true,
data: "Article: " + req.params.id + "deleted successfully"
})
}
})
}
return this;
}
module.exports = new articleController();
Demo on GitHub
Demo 百度网盘:
链接: https://pan.baidu.com/s/1o7WtS8q 密码: sngj