Skip to content

Instantly share code, notes, and snippets.

//company.routes.js
const CompanyController = require('../controllers/company.controller');
module.exports = [
{
path: '/api/companies',
method: 'POST',
handler: CompanyController.create
},
{
path: '/api/companies',
//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,
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);
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 = {};
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);
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);
})
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);
})
//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);
}
//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
},
//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
});