blog starting point
This commit is contained in:
parent
a561131d0c
commit
8bb31432c7
6 changed files with 1108 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
.env
|
||||||
|
data/
|
||||||
28
docker-compose.yml
Normal file
28
docker-compose.yml
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
mongo:
|
||||||
|
image: mongo
|
||||||
|
environment:
|
||||||
|
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
|
||||||
|
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
|
||||||
|
- MONGO_INITDB_DATABASE=project
|
||||||
|
ports:
|
||||||
|
- '27017:27017'
|
||||||
|
volumes:
|
||||||
|
- ./data:/data/db
|
||||||
|
mongo-express:
|
||||||
|
image: mongo-express
|
||||||
|
environment:
|
||||||
|
- ME_CONFIG_MONGODB_SERVER=mongo
|
||||||
|
- ME_CONFIG_MONGODB_PORT=27017
|
||||||
|
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
|
||||||
|
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
|
||||||
|
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
|
||||||
|
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
|
||||||
|
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
|
||||||
|
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
|
||||||
|
depends_on:
|
||||||
|
- mongo
|
||||||
|
ports:
|
||||||
|
- "8888:8081"
|
||||||
2
parts/4/blogApp/.gitignore
vendored
Normal file
2
parts/4/blogApp/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules/
|
||||||
|
data/
|
||||||
1022
parts/4/blogApp/package-lock.json
generated
Normal file
1022
parts/4/blogApp/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
17
parts/4/blogApp/package.json
Normal file
17
parts/4/blogApp/package.json
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "blogapp",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node src/app.js",
|
||||||
|
"dev": "node --watch src/app.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"description": "",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"mongoose": "^8.15.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
37
parts/4/blogApp/src/app.js
Normal file
37
parts/4/blogApp/src/app.js
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
const express = require('express')
|
||||||
|
const mongoose = require('mongoose')
|
||||||
|
|
||||||
|
const app = express()
|
||||||
|
|
||||||
|
const blogSchema = mongoose.Schema({
|
||||||
|
title: String,
|
||||||
|
author: String,
|
||||||
|
url: String,
|
||||||
|
likes: Number,
|
||||||
|
})
|
||||||
|
|
||||||
|
const Blog = mongoose.model('Blog', blogSchema)
|
||||||
|
|
||||||
|
const mongoUrl = 'mongodb://localhost/bloglist'
|
||||||
|
mongoose.connect(mongoUrl)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
blog.save().then((result) => {
|
||||||
|
response.status(201).json(result)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const PORT = 3003
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`Server running on port ${PORT}`)
|
||||||
|
})
|
||||||
Loading…
Add table
Add a link
Reference in a new issue