const router = require('express').Router();
const mongoose = require('mongoose');
// Project model
const Project = require('../models/project');
// @routes
router
.route('/:id')
.all((req, res, next) => {
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
next(new Error(404, "Wrong project id"));
} else {
next();
}
})
.get((req, res, next) => {
Project.findById(req.params.id)
.then(project => res.json(project));
})
.put((req, res, next) => {
Project.findByIdAndUpdate(req.params.id, req.body, { new: true })
.then(project => res.json(project));
})
.delete((req, res, next) => {
Project.findByIdAndRemove(req.params.id)
.then(project => res.json(project));
})
.post((req, res, next) => {
next(new Error("not implemented!"));
});
router.get('/', (req, res, next) => {
Project.find()
.sort({ date: -1 })
.then(projects => res.json(projects))
.catch(next);
});
router.post('/', (req, res, next) => {
const newProject = new Project({
name: req.body.name,
description: req.body.description,
slides: []
});
newProject
.save()
.then(project => res.json(project))
.catch(next);
});
module.exports = router;
import Router from 'koa-router';
export const router = new Router();
import './example';
import './another-route';
import './one-more-route';
import { router } from '../../router';
router.get('/example', (ctx) => {
ctx.body = { yo: 'Yo dude' };
});
router.post('/example', (ctx) => {
ctx.body = { wow: 'Some POST request goes here' };
});
import Koa from 'koa';
import { router } from './router';
import './routes';
const app = new Koa();
// body parser etc
// app.use(someMiddleware());
// app.use(moreMiddleware());
app.use(router.routes(), router.allowedMethods());
app.listen(8000);
const mongoose = require('mongoose');
const api = {};
api.checkId = () => (req, res, next) => {
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
next(new Error(404, "Wrong project id"));
} else {
next();
}
};
api.get = (Project) => (req, res, next) => {
Project.findById(req.params.id)
.then(project => res.json(project));
};
api.update = (Project) => (req, res, next) => {
Project.findByIdAndUpdate(req.params.id, req.body, { new: true })
.then(project => res.json(project));
};
api.delete = (Project) => (req, res, next) => {
Project.findByIdAndRemove(req.params.id)
.then(project => res.json(project));
};
api.notImplement = () => (req, res, next) => {
next(new Error("not implemented!"));
};
api.all = (Project) => (req, res, next) => {
Project.find()
.sort({ date: -1 })
.then(projects => res.json(projects))
.catch(next);
};
api.create = (Project) => (req, res, next) => {
const newProject = new Project({
name: req.body.name,
description: req.body.description,
slides: []
});
newProject
.save()
.then(project => res.json(project))
.catch(next);
};
module.exports = api;
const router = require('express').Router();
const api = require('../api/project')
// Project model
const Project = require('../models/project');
// @routes
router
.route('/:id')
.all(api.checkId())
.get(api.get(Project))
.put(api.update(Project))
.delete(api.delete(Project))
.post(api.notImplement());
router.get('/', api.all(Project));
router.post('/', api.create(Project));
module.exports = router;
res.status(404).json({message: 'Project not found'}).end()