Welcome folks today in this post we will be building a rest api using deno and mongodb with oak framework.Below is a step by step youtube video for building this application. All the source code of the application is given below. Download the source code also below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import { Application,Router } from "https://deno.land/x/oak/mod.ts"; import {getNotes, createNote, getSingleNote, updateNote, deleteNote} from './routes.ts'; const router = new Router(); router .get('/', (ctx) => { ctx.response.body = 'Welcome to notes API'; }) .get('/notes', getNotes) .get('/notes/:id', getSingleNote) .post('/notes', createNote) .put('/notes/:id', updateNote) .delete('/notes/:id', deleteNote) ; const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); app.listen({port: 8000}); console.log("Server is up and running"); |
1 2 3 4 5 6 7 8 9 10 |
// import { MongoClient } from "https://deno.land/x/mongo@v0.8.0/mod.ts"; const MONGO_URL = `mongodb://localhost:27017` const client = new MongoClient(); client.connectWithUri(MONGO_URL); const db = client.database('notes'); export default db; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import { RouterContext } from "https://deno.land/x/oak/mod.ts"; import db from "./mongodb.ts"; const notesCollection = db.collection("notes"); const getNotes = async (ctx: RouterContext) => { // Get Notes from MongoDB const notes = await notesCollection.find(); // Return output ctx.response.body = notes; }; const getSingleNote = async (ctx: RouterContext) => { const id = ctx.params.id; // Get single note const note = await notesCollection.findOne({ _id: { $oid: id } }); // Return output ctx.response.body = note; }; const createNote = async (ctx: RouterContext) => { // Get title and body from request const { value: {title, body} } = await ctx.request.body(); // Create Note object const note: any = { title, body, date: new Date(), }; // Insert Note in MongoDB const id = await notesCollection.insertOne(note); note._id = id; // Return with success response ctx.response.status = 201; ctx.response.body = note; }; const updateNote = async (ctx: RouterContext) => { const id = ctx.params.id; // Get title and body from request const { value: {title, body} } = await ctx.request.body(); const { modifiedCount } = await notesCollection.updateOne( { _id: { $oid: id } }, { $set: { title, body, }, }, ); if (!modifiedCount) { ctx.response.status = 404; ctx.response.body = { message: "Note does not exist" }; return; } ctx.response.body = await notesCollection.findOne({ _id: { $oid: id } }); }; const deleteNote = async (ctx: RouterContext) => { const id = ctx.params.id; const count = await notesCollection.deleteOne({ _id: { $oid: id } }); if (!count) { ctx.response.status = 404; ctx.response.body = { message: "Note does not exist" }; return; } ctx.response.status = 204; }; export { getNotes, createNote, getSingleNote, updateNote, deleteNote }; |