楼主: ReneeBK
908 6

MEAN Blueprints [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

1论坛币
  1. MEAN Blueprints
  2. By: Robert Onodi
  3. Publisher: Packt Publishing
  4. Pub. Date: May 30, 2016
  5. Web ISBN-13: 978-1-78528-580-6
  6. Print ISBN-13: 978-1-78355-394-5
  7. Pages in Print Edition: 336
  8. Subscriber Rating: 0 out of 5 rating [0 Ratings]
复制代码


关键词:Blueprint eprints print EPRI Blue rating Robert
沙发
ReneeBK 发表于 2017-1-8 02:08:07 |只看作者 |坛友微信交流群
  1. Creating the contact mongoose schema
  2. Our system needs some sort of functionality to store the possible clients or just contact persons of other companies. For this, we are going to create a contact schema that will represent the same collection storing all the contacts in MongoDB. We are going to keep our contact schema simple. Let's create a model file in contact-manager/app/models/contact.js, which will hold the schema, and add the following code to it:

  3. 'use strict';

  4. const mongoose = require('mongoose');
  5. const Schema = mongoose.Schema;

  6. var ContactSchema = new Schema({
  7.   email:  {
  8.     type: String
  9.   },
  10.   name: {
  11.     type: String
  12.   },
  13.   city: {
  14.     type: String
  15.   },
  16.   phoneNumber: {
  17.     type: String
  18.   },
  19.   company: {
  20.     type: String
  21.   },
  22.   createdAt: {
  23.     type: Date,
  24.     default: Date.now
  25.   }
  26. });

  27. // compile and export the Contact model
  28. module.exports = mongoose.model('Contact', ContactSchema);
复制代码

使用道具

藤椅
ReneeBK 发表于 2017-1-8 02:08:38 |只看作者 |坛友微信交流群

Describing the contact route

  1. 'use strict';

  2. /**
  3. * Important! Set the environment to test
  4. */
  5. process.env.NODE_ENV = 'test';

  6. const http = require('http');
  7. const request = require('request');
  8. const chai = require('chai');
  9. const userFixture = require('../fixtures/user');
  10. const should = chai.should();

  11. let app;
  12. let appServer;
  13. let mongoose;
  14. let User;
  15. let Contact;
  16. let config;
  17. let baseUrl;
  18. let apiUrl;

  19. describe('Contacts endpoints test', function() {

  20.   before((done) => {
  21.     // boot app
  22.     // start listening to requests
  23.   });

  24.   after(function(done) {
  25.     // close app
  26.     // cleanup database
  27.     // close connection to mongo
  28.   });

  29.   afterEach((done) => {
  30.     // remove contacts
  31.   });

  32.   describe('Save contact', () => {});

  33.   describe('Get contacts', () => {});

  34.   describe('Get contact', function() {});

  35.   describe('Update contact', function() {});

  36.   describe('Delete contact', function() {});
  37. });
复制代码

使用道具

板凳
ReneeBK 发表于 2017-1-8 02:09:54 |只看作者 |坛友微信交流群
  1. Implementing the contact routes
  2. Now, we'll start implementing the contact CRUD operations. We'll begin by creating our controller. Create a new file, contact-manager/app/controllers/contact.js, and add the following code:

  3. 'use strict';

  4. const _ = require('lodash');
  5. const mongoose = require('mongoose');
  6. const Contact = mongoose.model('Contact');
  7. const ObjectId = mongoose.Types.ObjectId;

  8. module.exports.create = createContact;
  9. module.exports.findById = findContactById;
  10. module.exports.getOne = getOneContact;
  11. module.exports.getAll = getAllContacts;
  12. module.exports.update = updateContact;
  13. module.exports.remove = removeContact;

  14. function createContact(req, res, next) {
  15.   Contact.create(req.body, (err, contact) => {
  16.     if (err) {
  17.       return next(err);
  18.     }

  19.     res.status(201).json(contact);
  20.   });
  21. }
复制代码

使用道具

报纸
ReneeBK 发表于 2017-1-8 02:10:28 |只看作者 |坛友微信交流群
  1. Running the contact test
  2. At this point, we should have implemented all the requirements for managing contacts on the backend. To test everything, we run the following command:

  3. $ mocha tests/integration/contact.test.js
复制代码

使用道具

地板
ReneeBK 发表于 2017-1-8 02:11:31 |只看作者 |坛友微信交流群
  1. Installing the dependencies
  2. Let's start by creating our package.json file in the root of the project and adding the following code:

  3. {
  4.   "name": "mean-blueprints-expensetracker",
  5.   "version": "0.0.1",
  6.   "repository": {
  7.     "type": "git",
  8.     "url": "https://github.com/robert52/mean-blueprints-expensetracker.git"
  9.   },
  10.   "engines": {
  11.     "node": ">=0.12.0"
  12.   },
  13.   "scripts": {
  14.     "start": "node app.js",
  15.     "unit": "mocha tests/unit/ --ui bdd --recursive --reporter spec --timeout 10000 --slow 900",
  16.     "integration": "mocha tests/integration/ --ui bdd --recursive --reporter spec --timeout 10000 --slow 900"
  17.   },
  18.   "dependencies": {
  19.     "async": "^0.9.0",
  20.     "body-parser": "^1.12.3",
  21.     "express": "^4.12.4",
  22.     "express-session": "^1.11.2",
  23.     "lodash": "^3.7.0",
  24.     "method-override": "^2.3.2",
  25.     "mongoose": "^4.0.2",
  26.     "passport": "^0.2.1",
  27.     "passport-local": "^1.0.0",
  28.     "serve-static": "^1.9.2"
  29.   },
  30.   "devDependencies": {
  31.     "chai": "^2.3.0",
  32.     "chai-things": "^0.2.0",
  33.     "mocha": "^2.2.4",
  34.     "request": "^2.55.0"
  35.   }
  36. }
复制代码

使用道具

7
ReneeBK 发表于 2017-1-8 02:12:04 |只看作者 |坛友微信交流群
  1. Creating the main server.js file
  2. The main entry point for our application is the server.js file. Create it in the root of the project. This file starts the web server and bootstraps all of the logic. Add the following lines of code:

  3. 'use strict';

  4. // Get process environment or set default environment to development
  5. const ENV = process.env.NODE_ENV || 'development';
  6. const DEFAULT_PORT = 3000;
  7. const DEFAULT_HOSTNAME = 'localhost';

  8. const http = require('http');
  9. const express = require('express');
  10. const config = require('./config');
  11. const app = express();
  12. let server;

  13. /**
  14. * Set express (app) variables
  15. */
  16. app.set('config', config);
  17. app.set('root', __dirname);
  18. app.set('env', ENV);

  19. require('./config/mongoose').init(app);
  20. require('./config/models').init(app);
  21. require('./config/passport').init(app);
  22. require('./config/express').init(app);
  23. require('./config/routes').init(app);

  24. app.use((err, req, res, next) => {
  25.   res.status(500).json(err);
  26. });

  27. /**
  28. * Start the app if not loaded by another module
  29. */
  30. if (!module.parent) {
  31.    server = http.createServer(app);
  32.    server.listen(
  33.      config.port || DEFAULT_PORT,
  34.      config.hostname || DEFAULT_HOSTNAME,
  35.      () => {
  36.        console.log(`${config.app.name} is running`);
  37.        console.log(`   listening on port: ${config.port}`);
  38.        console.log(`   environment: ${ENV.toLowerCase()}`);
  39.      }
  40.    );
  41. }

  42. module.exports = app;
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 05:18