diff options
-rw-r--r-- | part4/blog/controllers/blogs.js | 25 | ||||
-rw-r--r-- | part4/blog/models/blog.js | 19 | ||||
-rw-r--r-- | part4/blog/src/index.js | 40 | ||||
-rw-r--r-- | part4/blog/utils/logger.js | 11 | ||||
-rw-r--r-- | part4/blog/utils/middleware.js | 32 |
5 files changed, 96 insertions, 31 deletions
diff --git a/part4/blog/controllers/blogs.js b/part4/blog/controllers/blogs.js new file mode 100644 index 0000000..78540c8 --- /dev/null +++ b/part4/blog/controllers/blogs.js @@ -0,0 +1,25 @@ +const blogsRouter = require('express').Router() +const Blog = require('../models/blog') + +blogsRouter.get('/', (request, response) => { + Blog + .find({}) + .then(blogs => { + response.json(blogs) + }) + .catch((error) => { + console.log(error.message) + }) +}) + +blogsRouter.post('/', (request, response) => { + const blog = new Blog(request.body) + + blog + .save() + .then(result => { + response.status(201).json(result) + }) +}) + +module.exports = blogsRouter diff --git a/part4/blog/models/blog.js b/part4/blog/models/blog.js new file mode 100644 index 0000000..28a68e9 --- /dev/null +++ b/part4/blog/models/blog.js @@ -0,0 +1,19 @@ +const mongoose = require('mongoose') + +const blogSchema = new mongoose.Schema({ + title: String, + author: String, + url: String, + likes: Number +}) + +blogSchema.set('toJSON', { + transform: (document, returnedObject) => { + returnedObject.id = returnedObject._id.toString() + delete returnedObject._id + delete returnedObject.__v + } +}) + + +module.exports = mongoose.model('Blog', blogSchema) diff --git a/part4/blog/src/index.js b/part4/blog/src/index.js index ff25ad4..7c56c4e 100644 --- a/part4/blog/src/index.js +++ b/part4/blog/src/index.js @@ -1,47 +1,25 @@ const express = require('express') const app = express() const cors = require('cors') +const Blog = require('../models/blog.js') +const blogsRouter = require('../controllers/blogs') const mongoose = require('mongoose') +const logger = require('../utils/logger.js') -const blogSchema = new mongoose.Schema({ - title: String, - author: String, - url: String, - likes: Number -}) - -const Blog = mongoose.model('Blog', blogSchema) -console.log("Connecting to mongo server") -const mongoUrl = 'somewhereLOL' +logger.info("Connecting to mongo server") +const mongoUrl = 'hello' mongoose.connect(mongoUrl) -console.log("Successfully connected to mongo server") +logger.info("Successfully connected to mongo server") app.use(cors()) app.use(express.json()) -app.get('/api/blogs', (request, response) => { - Blog - .find({}) - .then(blogs => { - response.json(blogs) - }) - .catch((error) => { - console.log(error.message) - }) -}) +app.get('/api/blogs', blogsRouter) -app.post('/api/blogs', (request, response) => { - const blog = new Blog(request.body) - - blog - .save() - .then(result => { - response.status(201).json(result) - }) -}) +app.post('/api/blogs', blogsRouter) const PORT = 3003 app.listen(PORT, () => { - console.log(`Server running on port ${PORT}`) + logger.info(`Server running on port ${PORT}`) }) diff --git a/part4/blog/utils/logger.js b/part4/blog/utils/logger.js new file mode 100644 index 0000000..f4b4c78 --- /dev/null +++ b/part4/blog/utils/logger.js @@ -0,0 +1,11 @@ +const info = (...params) => { + console.log('info: ', ...params) +} + +const error = (...params) => { + console.log('error: ', ...params) +} + +module.exports = { + info, error +} diff --git a/part4/blog/utils/middleware.js b/part4/blog/utils/middleware.js new file mode 100644 index 0000000..1b2979a --- /dev/null +++ b/part4/blog/utils/middleware.js @@ -0,0 +1,32 @@ +const logger = require('./logger.js') + +const requestLogger = (request, response, next) => { + logger.info('Method:', request.method) + logger.info('Path: ', request.path) + logger.info('Body: ', request.body) + logger.info('---') + next() +} + +const unknownEndpoint = (request, response) => { + response.status(404).send({ error: 'unkown endpoint' }) +} + +const errorHandler = (error, request, response, next) => { + logger.error(error.message) + + if (error.name === 'CastError') { + return response.status(400).send( {error: 'malformatted id'} ) + + } else if (error.name === 'ValidationError') { + return response.status(400).json({error: 'error.message'}) + } + + next(error) +} + +module.exports = { + requestLogger, + unknownEndpoint, + errorHandler +} |