-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1db98c0
commit c7243ee
Showing
9 changed files
with
6,675 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const conf = require('./utils/config') | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
const app = express() | ||
const blogsRouter = require('./controllers/blogs') | ||
const mongoose = require('mongoose') | ||
|
||
console.log('connecting to', conf.mongoUrl) | ||
|
||
mongoose.connect(conf.mongoUrl, { useNewUrlParser: true }) | ||
.then(() => { | ||
console.log('connected to MongoDB') | ||
}) | ||
.catch((error) => { | ||
console.log('error connection to MongoDB:', error.message) | ||
}) | ||
|
||
app.use(express.static('build')) | ||
app.use(bodyParser.json()) | ||
|
||
app.use('/api/blogs', blogsRouter) | ||
|
||
module.exports = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const blogsRouter = require('express').Router() | ||
const mongoose = require('mongoose') | ||
|
||
|
||
const blogSchema = mongoose.Schema({ | ||
title: String, | ||
author: String, | ||
url: String, | ||
likes: Number | ||
}) | ||
|
||
const Blog = mongoose.model('Blog', blogSchema) | ||
|
||
blogsRouter.get('/', (request, response) => { | ||
Blog | ||
.find({}) | ||
.then(blogs => { | ||
response.json(blogs) | ||
}) | ||
}) | ||
|
||
blogsRouter.post('/', (request, response) => { | ||
const blog = new Blog(request.body) | ||
|
||
blog | ||
.save() | ||
.then(result => { | ||
response.status(201).json(result) | ||
}) | ||
}) | ||
|
||
|
||
|
||
module.exports = blogsRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const app = require('./app') | ||
const conf = require('./utils/config') | ||
const http = require('http') | ||
|
||
app.listen(conf.PORT, () => { | ||
console.log(`Server running on port ${conf.PORT}`) | ||
}) |
Oops, something went wrong.