更新:
最新的源码和运用说明放到了github上 点击跳转
本文的重点在于restful接口的设想与完成,运用到了express和monogoose.
点击检察express教程
点击检察monogoose教程
ES6入门教程—-阮一峰的博客
node4.x对ES6的支撑并不完美,发起运用更高版本的node
在现实开辟中请在文件头部增加‘use strict’来声明运用严厉形式
为了有助于更好的明白后期的代码,我们起首来进修一下ES6的类与继续
ES6的类与继续
1.定义与运用
//定义一个基类
class BaseService{
add(){
console.log('base add');// base add
}
remove(){
console.log('base remove') // base remove
}
}
//实例化
var base = new BaseService();
base.add(); //输出 'base add'
2.继续
//重载基类的要领
//增加私有要领
class UserServie extends BaseService{
add(){
console.log('user add');// user add
}
findTop5(){
console.log('1,2,3,4,5');// 1,2,3,4,5
}
}
node的模块
其次进修一下node里的模块,假如要运用ES6的模块,你须要分外运用babel
1.导出基类
新建baseService.js
class Service{
add(){
console.log('base add');// base add
}
remove(){
console.log('base remove') // base remove
}
}
exports.service = Service;
2.在子类中援用基类
新建userService.js
var baseService = require('./baseService').service;
class Service extends baseService{
findTop5(){
console.log('1,2,3,4,5');// 1,2,3,4,5
}
add(){
console.log('user add');// user add
}
}
exports.service = Service;
搭建项目构造
下面将会运用到monogoose和express相干手艺,不懂的能够看本文顶部链接
1.运用express初始化项目以后增加models文件夹和services文件夹
在models文件夹内新建userModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var model = new Schema({
name:String
});
exports.model = mongoose.model('userModel', model);
在services文件夹内新建baseService.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
class Service{
constructor(){
}
add(obj, name,callback){
var name = name || '';
var result = {};
var instance = this.entity(obj);
instance.save(function(err, item, numAffected) {
if (err) {
result = { 'ok': false, 'data': {}, 'message': '新增' + name + '失利' }
} else {
result = { 'ok': true, 'data': item, 'message': '新增' + name + '胜利' }
}
callback(result);
});
}
delete(obj, name,callback){
var result = {};
var id = obj.id;
this.entity.remove({ '_id': id }, function(err) {
if (err) {
result = { 'ok': false, 'data': {}, 'message': '删除' + name + '失利' };
} else {
result = { 'ok': true, "data": {}, 'message': '删除' + name + '胜利' };
}
callback(result);
});
}
}
exports.service = Service;
然后新建userService.js,援用对应的model,而且完成userService对baseService的继续
var baseService = require('./baseService').service;
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var model = require('../models/userModel').model;
class Service extends baseService{
constructor(){
super();
this.entity = model;
}
}
exports.service = Service;
在userService.js中我们能够重写基类的要领,也能够增加本身私有的要领和属性
在上文中已提到过,下面再来熟习一遍吧
假如要在子类中挪用this,须要先挪用super要领,不然新建实例时会报错。这是由于子类没有本身的this对象,而是继续父类的this对象,然后对其举行加工。假如不挪用super要领,子类就得不到this对象。
class Service extends baseService{
constructor(){
super();
this.entity = model;
}
add(){
console.log('user add');
}
findTop5(){
console.log('1,2,3,4,5');
}
}
接着再新建services.js文件来一致对外导出service ,下面代码是现实项目中的代码,仅作为参照
exports.userService = require('./userService').service;
exports.emptyService = require('./emptyService').service;
exports.themeService = require('./themeService').service;
exports.documentService = require('./documentService').service;
exports.chartTypeService = require('./chartTypeService').service;
exports.mapTypeService = require('./mapTypeService').service;
exports.pageService = require('./pageService').service;
exports.cellService = require('./cellService').service;
exports.defaultEchartTypeService = require('./defaultEchartTypeService').service;
exports.defaultMapTypeService = require('./defaultMapTypeService').service;
exports.issueService = require('./issueService').service;
末了是路由了,在routes文件夹内新建api.js文件
var express = require('express');
var router = express.Router();
var Services = require('../services/services');
mongoose.connect('mongodb://localhost/test');
/*
listen all
name 称号
method 要领
par 参数
entity 实体实例
service 效劳实例
result 返回效果
obj 要求参数
参数为空或许毛病时挪用empty
防备顺序崩溃
*/
router.post('/:name?/:method?/:par?',function(req,res,next){
var name = req.params.name || 'empty';
var method = req.params.method || 'empty';
var par = req.params.par || '';
var service = new Services[name+'Service']();
var obj = req.body || {};
obj.par = par;
if(!Services[name+'Service']){
service = new Services.emptyService();
}
service[method](obj,name,function(result){
res.json(result);
});
return;
});
module.exports = router;
在代码中,我们援用了Services,
起首经由过程new Services[name+'Service']()
来实例化对应的service
然后挪用service[method]
要领,
个中:name?、:method?、:par?
为占位符
在前端页面中,我们能够运用$.post('/api/user/add',data,function(){})
这个典范的要领来测试一下API是不是能够一般运转,作者比较喜好将参数悉数放在data中
有疑问或许不解的能够发邮件到http_wenwen@163.com