blog starting point

This commit is contained in:
Pablo Martin 2025-06-04 17:23:22 +02:00
parent a561131d0c
commit 8bb31432c7
6 changed files with 1108 additions and 0 deletions

2
parts/4/blogApp/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules/
data/

1022
parts/4/blogApp/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View 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"
}
}

View 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}`)
})