Last active
December 6, 2019 09:38
-
-
Save haimrait/39d3d3cbafbd84d2bbc340627dbe524e to your computer and use it in GitHub Desktop.
node-redis-mongo - book routes version before cache layer added.
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
const mongoose = require("mongoose"); | |
const Book = mongoose.model("Book"); | |
module.exports = app => { | |
app.get("/api/books", async (req, res) => { | |
let books; | |
if (req.query.author) { | |
books = await Book.find({ author: req.query.author }) | |
} else { | |
books = await Book.find() | |
} | |
res.send(books); | |
}); | |
app.post("/api/books", async (req, res) => { | |
const { title, content, author } = req.body; | |
const book = new Book({ | |
title, | |
content, | |
author | |
}); | |
try { | |
await book.save(); | |
res.send(book); | |
} catch (err) { | |
res.send(400, err); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment