Skip to content

Commit

Permalink
Add models
Browse files Browse the repository at this point in the history
  • Loading branch information
Prathamesh More committed Sep 18, 2019
1 parent 9f2902c commit 6195fce
Show file tree
Hide file tree
Showing 4 changed files with 590 additions and 1 deletion.
55 changes: 55 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const express = require("express");
const mongoose = require("mongoose");
const app = express();

//Imports
const Quote = require("./models/quote");

//Consts
const PORT = 3000;
const URL = "192.168.43.61";

//Database connection
mongoose.connect("mongodb://localhost:27017/quotegarden", { useNewUrlParser: true }, (error) => {
if (error) {
console.log("Error");
} else {
console.log("Successfully connected to database");
}
});

//Simple GET method
app.get("/", (request, response) => {
response.send("Hello from API");
})

//Simple random requests
app.get("/random", async (request, response) => {

const documentCount = await Quote.estimatedDocumentCount();
const index = random

Quote.find({}, (error, quote) => {
if (error) {
console.log("Error");
} else {
response.json(quote);
}
})
})

//Routes to find as per search
app.get("/:author", (request, response) => {
response.send("All Authors");
});

//Get all quotes
app.get("/all", (request, response) => {
response.send("All Quotes");
})


//Start server
app.listen(PORT, () => {
console.log("Server running on port : " + PORT)
})
12 changes: 12 additions & 0 deletions models/quote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require("mongoose");

//Define schema for quotes
const quoteSchema = new mongoose.Schema({
content: String,
author: String
});

//Create model on the basis of quote schema
const Quote = new mongoose.model("quote", quoteSchema);

module.exports = Quote;
Loading

0 comments on commit 6195fce

Please sign in to comment.