Skip to content

Instantly share code, notes, and snippets.

@haimrait
Last active December 6, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haimrait/39d3d3cbafbd84d2bbc340627dbe524e to your computer and use it in GitHub Desktop.
Save haimrait/39d3d3cbafbd84d2bbc340627dbe524e to your computer and use it in GitHub Desktop.
node-redis-mongo - book routes version before cache layer added.
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