This commit is contained in:
Pablo Martin 2025-06-04 17:44:52 +02:00
parent 41ec39c961
commit 09a320a5da
2 changed files with 18 additions and 19 deletions

View file

@ -1,27 +1,26 @@
const express = require('express') const express = require("express");
const db = require('./db') const { models } = require("./db");
const app = express() const app = express();
const Blog = db.Blog const Blog = models.Blog;
app.use(express.json());
app.use(express.json()) app.get("/api/blogs", (request, response) => {
app.get('/api/blogs', (request, response) => {
Blog.find({}).then((blogs) => { Blog.find({}).then((blogs) => {
response.json(blogs) response.json(blogs);
}) });
}) });
app.post('/api/blogs', (request, response) => { app.post("/api/blogs", (request, response) => {
const blog = new Blog(request.body) const blog = new Blog(request.body);
blog.save().then((result) => { blog.save().then((result) => {
response.status(201).json(result) response.status(201).json(result);
}) });
}) });
const PORT = 3003 const PORT = 3003;
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`) console.log(`Server running on port ${PORT}`);
}) });

View file

@ -12,4 +12,4 @@ const Blog = mongoose.model('Blog', blogSchema)
const mongoUrl = 'mongodb://localhost/bloglist' const mongoUrl = 'mongodb://localhost/bloglist'
mongoose.connect(mongoUrl) mongoose.connect(mongoUrl)
module.exports = {Blog} module.exports = {models: {Blog}}