Skip to content

Commit

Permalink
answer 4.1 Blog list, step1
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 9, 2023
1 parent 8de8f93 commit 01104c7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
node_modules
.env
build
dist
33 changes: 29 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
const config = require('./utils/config')
const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require('mongoose')

const app = express()
const blogSchema = new mongoose.Schema({
title: String,
author: String,
url: String,
likes: Number,
})

const Blog = mongoose.model('Blog', blogSchema)

mongoose.connect(config.MONGODB_URI)

app.use(cors())
app.use(express.json())

app.get('/api/blogs', (request, response) => {
Blog.find({}).then((blogs) => {
response.json(blogs)
})
})

app.post('/api/blogs', (request, response) => {
const blog = new Blog(request.body)

app.get('/', (request, response) => {
response.send('<h1>Hello World!</h1>')
blog.save().then((result) => {
response.status(201).json(result)
})
})

const PORT = process.env.PORT || 3001
const PORT = config.PORT
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`)
})
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"homepage": "https://github.com/patchamama/fullstackopen-part4-bloglist#readme",
"dependencies": {
"cors": "2.8.5",
"dotenv": "16.3.1",
"express": "4.18.2",
"mongoose": "7.5.0"
},
Expand Down
12 changes: 12 additions & 0 deletions requests/create_blog_entry.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST http://localhost:3003/api/blogs
Content-Type: application/json

{
"title": "Test of blog title",
"author": "Test of blog author",
"url": "Test of blog url",
"likes": 0
}

###
GET http://localhost:3003/api/blogs/
9 changes: 9 additions & 0 deletions utils/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require('dotenv').config()

const PORT = process.env.PORT
const MONGODB_URI = process.env.MONGODB_URI

module.exports = {
MONGODB_URI,
PORT,
}

0 comments on commit 01104c7

Please sign in to comment.