Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Hit enter after type your search item

Deno & MongoDB REST API Tutorial with Oak Framework

/
/
/
123 Views

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.

 

 

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");

 

// 
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;

 

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 };

 

READ  Node.js Webcam Easy JS Library to Take Photos From Webcam in Browser Using Javascript Full App

DOWNLOAD SOURCE CODE

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :