blog starting point
This commit is contained in:
parent
a561131d0c
commit
8bb31432c7
6 changed files with 1108 additions and 0 deletions
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