aboutsummaryrefslogtreecommitdiff
path: root/part4/blog/src/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'part4/blog/src/index.js')
-rw-r--r--part4/blog/src/index.js47
1 files changed, 47 insertions, 0 deletions
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}`)
+})