From 01104c7798ad53f35ee73994826f5cc3602b9b3e Mon Sep 17 00:00:00 2001 From: "A. Urquiola" Date: Sat, 9 Sep 2023 14:46:12 +0200 Subject: [PATCH] answer 4.1 Blog list, step1 --- .gitignore | 2 -- index.js | 33 +++++++++++++++++++++++++++++---- package-lock.json | 12 ++++++++++++ package.json | 1 + requests/create_blog_entry.rest | 12 ++++++++++++ utils/config.js | 9 +++++++++ 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 requests/create_blog_entry.rest create mode 100644 utils/config.js diff --git a/.gitignore b/.gitignore index e66b056..37d7e73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ node_modules .env -build -dist \ No newline at end of file diff --git a/index.js b/index.js index f8c5796..47e8563 100644 --- a/index.js +++ b/index.js @@ -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('

Hello World!

') + 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}`) }) diff --git a/package-lock.json b/package-lock.json index 4c65923..cc4e65c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "cors": "2.8.5", + "dotenv": "16.3.1", "express": "4.18.2", "mongoose": "7.5.0" }, @@ -614,6 +615,17 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/package.json b/package.json index 7a9afc9..bcd455a 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/requests/create_blog_entry.rest b/requests/create_blog_entry.rest new file mode 100644 index 0000000..d2baaf5 --- /dev/null +++ b/requests/create_blog_entry.rest @@ -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/ \ No newline at end of file diff --git a/utils/config.js b/utils/config.js new file mode 100644 index 0000000..1189e9e --- /dev/null +++ b/utils/config.js @@ -0,0 +1,9 @@ +require('dotenv').config() + +const PORT = process.env.PORT +const MONGODB_URI = process.env.MONGODB_URI + +module.exports = { + MONGODB_URI, + PORT, +}