55 lines
1.0 KiB
JavaScript
55 lines
1.0 KiB
JavaScript
var express = require('express')
|
|
var bodyParser = require('body-parser')
|
|
const {ObjectID} = require('mongodb');
|
|
|
|
|
|
var {mongoose} = require('./db/mongoose')
|
|
var {User} = require('./models/user')
|
|
var {Todo} = require('./models/todo')
|
|
|
|
var app = express();
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.post('/todos', (req, res) => {
|
|
var todo = new Todo({
|
|
text: req.body.text
|
|
});
|
|
|
|
todo.save().then((doc) => {
|
|
return res.send(doc);
|
|
}, (e) => {
|
|
return res.status(400).send(e);
|
|
});
|
|
});
|
|
|
|
app.get('/todos', (req, res) => {
|
|
Todo.find().then((todos) => {
|
|
return res.send({todos});
|
|
}, (e) => {
|
|
return res.status(400).send(e);
|
|
});
|
|
});
|
|
|
|
app.get('/todos/:id', (req, res) => {
|
|
var id = req.params.id;
|
|
if (!ObjectID.isValid(id)){
|
|
return res.status(404).send();
|
|
}
|
|
|
|
Todo.findById(req.params.id).then((todo) => {
|
|
if(!todo){
|
|
return res.status(404).send();
|
|
}
|
|
return res.send({todo});
|
|
}).catch((e) => res.status(400).send())
|
|
});
|
|
|
|
|
|
|
|
app.listen(8888, () => {
|
|
console.log("Started on port 8888");
|
|
});
|
|
|
|
module.exports = {app}
|