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

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