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
| //import fastify & mongoose | |
| const fastify = require('fastify'); | |
| const mongoose = require('mongoose'); | |
| //initialized Fastify App | |
| const app = fastify(); | |
| //connected fastify to mongoose | |
| try { | |
| mongoose.connect('mongodb://localhost:27017/notes_db'); |
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 Note = require('../models/note'); | |
| module.exports = (request, reply, done) => { | |
| Note.count({}, (err, count) => { | |
| if (err) { | |
| console.error(err); | |
| reply.code(500).send('Error!'); | |
| } | |
| reply.header('Content-Range', `notes 0-10}/${count}`); | |
| done(); |
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 Note = require('../models/note'); | |
| module.exports = { | |
| //# create a note | |
| create: async (request, reply) => { | |
| try { | |
| const note = request.body; | |
| const newNote = await Note.create(note); | |
| reply.code(201).send(newNote); | |
| } catch (e) { |
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
| //# create a note | |
| create: async (request, reply) => { | |
| try { | |
| const note = request.body; | |
| const newNote = await Note.create(note); | |
| reply.code(201).send(newNote); | |
| } catch (e) { | |
| reply.code(500).send(e); | |
| } | |
| }, |
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
| //#get the list of notes | |
| fetch: async (request, reply) => { | |
| try { | |
| const notes = await Note.find({}); | |
| reply.code(200).send(notes); | |
| } catch (e) { | |
| reply.code(500).send(e); | |
| } | |
| }, |
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
| //#get a single note | |
| get: async (request, reply) => { | |
| try { | |
| const noteId = request.params.id; | |
| const note = await Note.findById(noteId); | |
| reply.code(200).send(note); | |
| } catch (e) { | |
| reply.code(500).send(e); | |
| } | |
| }, |
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
| //#update a note | |
| update: async (request, reply) => { | |
| try { | |
| const noteId = request.params.id; | |
| const updates = request.body; | |
| await Note.findByIdAndUpdate(noteId, updates); | |
| const noteToUpdate = await Note.findById(noteId); | |
| reply.code(200).send({ data: noteToUpdate }); | |
| } catch (e) { | |
| reply.code(500).send(e); |
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
| //#delete a note | |
| delete: async (request, reply) => { | |
| try { | |
| const noteId = request.params.id; | |
| const noteToDelete = await Note.findById(noteId); | |
| await Note.findByIdAndDelete(noteId); | |
| reply.code(200).send({ data: noteToDelete }); | |
| } catch (e) { | |
| reply.code(500).send(e); | |
| } |
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 Note = require('../models/note'); | |
| module.exports = { | |
| //# create a note | |
| create: async (request, reply) => {}, | |
| //#get the list of notes | |
| fetch: async (request, reply) => {}, | |
| //#get a single note |
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 notesController = require('../controllers/notesController'); | |
| module.exports = (app) => { | |
| //# create a note | |
| app.post('/api/notes', notesController.create); | |
| //#get the list of notes | |
| app.get('/api/notes', notesController.fetch); | |
| //#get a single note |
OlderNewer