From 13ca52505d4a247f72c5fad8161dc497da6da368 Mon Sep 17 00:00:00 2001 From: Ibrahim Mkusa Date: Sun, 18 Jun 2023 02:19:25 -0400 Subject: ex4.1 fixed secrets bobo --- part4/blog/src/index.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 part4/blog/src/index.js diff --git a/part4/blog/src/index.js b/part4/blog/src/index.js new file mode 100644 index 0000000..ff25ad4 --- /dev/null +++ b/part4/blog/src/index.js @@ -0,0 +1,47 @@ +const express = require('express') +const app = express() +const cors = require('cors') +const mongoose = require('mongoose') + +const blogSchema = new mongoose.Schema({ + title: String, + author: String, + url: String, + likes: Number +}) + +const Blog = mongoose.model('Blog', blogSchema) + +console.log("Connecting to mongo server") +const mongoUrl = 'somewhereLOL' +mongoose.connect(mongoUrl) +console.log("Successfully connected to mongo server") + +app.use(cors()) +app.use(express.json()) + +app.get('/api/blogs', (request, response) => { + Blog + .find({}) + .then(blogs => { + response.json(blogs) + }) + .catch((error) => { + console.log(error.message) + }) +}) + +app.post('/api/blogs', (request, response) => { + const blog = new Blog(request.body) + + blog + .save() + .then(result => { + response.status(201).json(result) + }) +}) + +const PORT = 3003 +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`) +}) -- cgit v1.2.3