This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //company.routes.js | |
| const CompanyController = require('../controllers/company.controller'); | |
| module.exports = [ | |
| { | |
| path: '/api/companies', | |
| method: 'POST', | |
| handler: CompanyController.create | |
| }, | |
| { | |
| path: '/api/companies', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //company.controller.js | |
| const Company = require('../models/company.model'); | |
| module.exports = { | |
| create(req, reply) { | |
| if (!req.payload.name) { | |
| return reply({er: 'name is required field'}).code(400); | |
| } | |
| Company.create({ | |
| name: req.payload.name, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.route({ | |
| path: '/api/companies/{id}', | |
| method: 'DELETE', | |
| handler(req, reply) { | |
| if (!req.params.id) { | |
| return reply({err: 'id is required param'}).code(400); | |
| } | |
| Company.findByIdAndRemove(req.params.id, (err, result) => { | |
| if (err) { | |
| return reply(err).code(500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.route({ | |
| path: '/api/companies/{id}', | |
| method: 'PUT', | |
| handler(req, reply) { | |
| if (!req.params.id) { | |
| return reply({err: 'id is required param'}).code(400); | |
| } | |
| let attributes = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.route({ | |
| path: '/api/companies/{id}', | |
| method: 'GET', | |
| handler(req, reply) { | |
| if (!req.params.id) { | |
| return reply({err: 'id is required param'}).code(400); | |
| } | |
| Company.findById(req.params.id, (err, company) => { | |
| if (err) { | |
| return reply(err).code(404); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.route({ | |
| path: '/api/companies', | |
| method: 'GET', | |
| handler(req, reply) { | |
| Company.find({}, (err, companies) => { | |
| if (err) { | |
| return reply(err).code(404); | |
| } | |
| return reply.response(companies); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.route({ | |
| path: '/api/companies', | |
| method: 'GET', | |
| handler(req, reply) { | |
| Company.find({}, (err, companies) => { | |
| if (err) { | |
| return reply(err).code(404); | |
| } | |
| return reply.response(companies); | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //server.js | |
| //... | |
| const Company = require('./models/company.model'); | |
| server.route({ | |
| path: '/api/companies', | |
| method: 'POST', | |
| handler(req, reply) { | |
| if (!req.payload.name) { | |
| return reply({er: 'name is required field'}).code(400); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //import the mongoose package | |
| const mongoose = require('mongoose'); | |
| //get the Schema class | |
| const Schema = mongoose.Schema; | |
| const CompanySchema = new Schema({ | |
| name: { | |
| required: true, | |
| type: String | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //server.js | |
| const hapi = require('hapi'); | |
| const server = new hapi.Server(); | |
| const mongoose = require('mongoose'); | |
| const mongoDbUri = 'mongodb://localhost:27017/hapi_db'; | |
| //connect with mongoDB | |
| mongoose.connect(mongoDbUri, { | |
| useMongoClient: true | |
| }); |