博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[MEAN Stack] First API -- 4. Organize app structure
阅读量:5367 次
发布时间:2019-06-15

本文共 2836 字,大约阅读时间需要 9 分钟。

The app structure

 

Front-end: app.js

/** * Created by Answer1215 on 12/9/2014. */'use strict';function MainCtrl(PeopleService) {    var vm = this;    vm.people = [];    vm.selectedPerson = {};   vm.getPeople = PeopleService.getPeople().then(function(response) {        vm.people = response.data;    });    vm.selectPerson = function(person) {        PeopleService.getSelectedPerson(person).then(function(response) {            vm.selectedPerson = response.data;            vm.selectedPerson.fullName = response.data.firstName + " " + response.data.lastName;        });    }}function PeopleService($http) {    var PeopleService = {};    PeopleService.getPeople = function() {        return $http.get('http://localhost:3000/people');    }    PeopleService.getSelectedPerson = function(person) {        return $http.get('http://localhost:3000/people/'+person._id);    }    return PeopleService;}angular.module('app',[    'ngResource'])    .controller('MainCtrl', MainCtrl)    .service('PeopleService', PeopleService);
    
  • {
    {person.firstName}}

{
{vm.selectedPerson.fullName}}

{
{vm.selectedPerson.email}}

 

Backend:

server.js

'use strict';var express = require('express');var cors = require("cors");var app = express();app.use(cors());var people = require('./controller/people');app.get('/people', people.getAll);app.get('/people/:id', people.get);app.listen(3000);

controller/people.js

/** * Created by Answer1215 on 12/26/2014. */var model = require('../model/people');exports.getAll = function(request, response){    model.getAll(function(err, data) {        if(err) response.json(503, {error: true});        response.json(200, data);    });}exports.get = function(request, response){    var id = request.params.id;    model.get(id, function(err, data) {        if(err) response.json(503, {error: true});        response.json(200, data);    });}

model/people.js

/** * Created by Answer1215 on 12/26/2014. */var mongoose = require('../lib/mogoose');var personSchema = {    firstName:String,    lastName:String,    email:String};//create a person model, and rename db as peoplevar Person = mongoose.model('Person', personSchema);exports.getAll = function(callback) {    Person.find().select("firstName").exec(function(err, data) {       // response.json(200, data);        callback(err, data);    });}exports.get = function(id, callback){    Person.findById(id, function(err, data) {        //response.send(data);        callback(err, data);    });}

lib/mogoose.js

/** * Created by Answer1215 on 12/26/2014. */var mongoose = require('mongoose');mongoose.connect('mongodb://localhost/simple');module.exports = mongoose;

 

转载于:https://www.cnblogs.com/Answer1215/p/4187965.html

你可能感兴趣的文章
ThinkPHP - Widget 工具
查看>>
前端图片上传预览
查看>>
(ZZ)ACM之歌
查看>>
Mecanim高级主题:Mecanim Blend Tree应用、Blend Tree 选项、复合Blend Tree
查看>>
分页/pagination
查看>>
HOJ Funfair
查看>>
web前端使用localstorage、sessionstorage、cookie增删获方法
查看>>
不要轻视行动的力量
查看>>
Python中re的match、search、findall、finditer区别
查看>>
网页制作中的超链接怎么做
查看>>
PHP类和对象之定义类的方法
查看>>
索引、视图、事务
查看>>
201671030106 词频统计软件项目报告
查看>>
Java作业(六)
查看>>
微信小程序播放背景音乐
查看>>
学号 20175223 《Java程序设计》第 5 周学习总结
查看>>
es6的find和findIndex方法
查看>>
Java中的interface接口
查看>>
常见linux命令释义(第五天)——shell变量学习
查看>>
Dell服务器R320在Centos6.5系统上安装MegaCli管理主板集成磁盘阵列卡
查看>>