Upload 4.2
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@
|
|||||||
part3/phonebook/.env
|
part3/phonebook/.env
|
||||||
part3/phonebook/node_modules
|
part3/phonebook/node_modules
|
||||||
test
|
test
|
||||||
|
part4/bloglist/.env
|
||||||
|
part4/bloglist/node_modules
|
||||||
|
|||||||
1
part4/bloglist/.eslintignore
Normal file
1
part4/bloglist/.eslintignore
Normal file
@@ -0,0 +1 @@
|
|||||||
|
build
|
||||||
40
part4/bloglist/.eslintrc.js
Normal file
40
part4/bloglist/.eslintrc.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
module.exports = {
|
||||||
|
'env': {
|
||||||
|
'node': true,
|
||||||
|
'commonjs': true,
|
||||||
|
'es2021': true
|
||||||
|
},
|
||||||
|
'extends': 'eslint:recommended',
|
||||||
|
'overrides': [
|
||||||
|
],
|
||||||
|
'parserOptions': {
|
||||||
|
'ecmaVersion': 'latest'
|
||||||
|
},
|
||||||
|
'rules': {
|
||||||
|
'indent': [
|
||||||
|
'error',
|
||||||
|
2
|
||||||
|
],
|
||||||
|
'linebreak-style': [
|
||||||
|
'error',
|
||||||
|
'unix'
|
||||||
|
],
|
||||||
|
'quotes': [
|
||||||
|
'error',
|
||||||
|
'single'
|
||||||
|
],
|
||||||
|
'semi': [
|
||||||
|
'error',
|
||||||
|
'never'
|
||||||
|
],
|
||||||
|
'eqeqeq': 'error',
|
||||||
|
'no-trailing-spaces': 'error',
|
||||||
|
'object-curly-spacing': [
|
||||||
|
'error', 'always'
|
||||||
|
],
|
||||||
|
'arrow-spacing': [
|
||||||
|
'error', { 'before': true, 'after': true }
|
||||||
|
],
|
||||||
|
'no-console': 0
|
||||||
|
}
|
||||||
|
}
|
||||||
32
part4/bloglist/app.js
Normal file
32
part4/bloglist/app.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
const config = require('./utils/config')
|
||||||
|
const express = require('express')
|
||||||
|
const app = express()
|
||||||
|
const cors = require('cors')
|
||||||
|
const blogsRouter = require('./controllers/blogs')
|
||||||
|
const middleware = require('./utils/middleware')
|
||||||
|
const logger = require('./utils/logger')
|
||||||
|
const mongoose = require('mongoose')
|
||||||
|
|
||||||
|
mongoose.set('strictQuery', false)
|
||||||
|
|
||||||
|
logger.info('connecting to', config.MONGODB_URI)
|
||||||
|
|
||||||
|
mongoose.connect(config.MONGODB_URI)
|
||||||
|
.then(() => {
|
||||||
|
logger.info('connected to MongoDB')
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
logger.error('error connecting to MongoDB:', error.message)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.use(cors())
|
||||||
|
app.use(express.static('build'))
|
||||||
|
app.use(express.json())
|
||||||
|
app.use(middleware.requestLogger)
|
||||||
|
|
||||||
|
app.use('/api/blogs', blogsRouter)
|
||||||
|
|
||||||
|
app.use(middleware.unknownEndpoint)
|
||||||
|
app.use(middleware.errorHandler)
|
||||||
|
|
||||||
|
module.exports = app
|
||||||
24
part4/bloglist/controllers/blogs.js
Normal file
24
part4/bloglist/controllers/blogs.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
const blogsRouter = require('express').Router()
|
||||||
|
const Blog = require('../models/blog')
|
||||||
|
|
||||||
|
blogsRouter.get('/', (request, response, next) => {
|
||||||
|
Blog
|
||||||
|
.find({})
|
||||||
|
.then(blogs => {
|
||||||
|
response.json(blogs)
|
||||||
|
})
|
||||||
|
.catch(error => next(error))
|
||||||
|
})
|
||||||
|
|
||||||
|
blogsRouter.post('/', (request, response, next) => {
|
||||||
|
const blog = new Blog(request.body)
|
||||||
|
console.log(request.body)
|
||||||
|
blog
|
||||||
|
.save()
|
||||||
|
.then(result => {
|
||||||
|
response.status(201).json(result)
|
||||||
|
})
|
||||||
|
.catch(error => next(error))
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = blogsRouter
|
||||||
7
part4/bloglist/index.js
Normal file
7
part4/bloglist/index.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const app = require('./app') // the actual Express application
|
||||||
|
const config = require('./utils/config')
|
||||||
|
const logger = require('./utils/logger')
|
||||||
|
|
||||||
|
app.listen(config.PORT, () => {
|
||||||
|
logger.info(`Server running on port ${config.PORT}`)
|
||||||
|
})
|
||||||
18
part4/bloglist/models/blog.js
Normal file
18
part4/bloglist/models/blog.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
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)
|
||||||
2285
part4/bloglist/package-lock.json
generated
Normal file
2285
part4/bloglist/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
23
part4/bloglist/package.json
Normal file
23
part4/bloglist/package.json
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "bloglist",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"dev": "nodemon index.js"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^16.1.1",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"mongoose": "^7.2.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"eslint": "^8.41.0",
|
||||||
|
"nodemon": "^2.0.22"
|
||||||
|
}
|
||||||
|
}
|
||||||
1
part4/bloglist/requests/get_all.rest
Normal file
1
part4/bloglist/requests/get_all.rest
Normal file
@@ -0,0 +1 @@
|
|||||||
|
GET http://localhost:3001/api/blogs
|
||||||
7
part4/bloglist/requests/post_blog.rest
Normal file
7
part4/bloglist/requests/post_blog.rest
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
POST http://localhost:3001/api/blogs
|
||||||
|
content-type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"title": "Another blog", "author": "Andrew", "url": "www.andrew.eu", "likes": 1000
|
||||||
|
|
||||||
|
}
|
||||||
9
part4/bloglist/utils/config.js
Normal file
9
part4/bloglist/utils/config.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
require('dotenv').config()
|
||||||
|
|
||||||
|
const PORT = process.env.PORT
|
||||||
|
const MONGODB_URI = process.env.MONGODB_URI
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
MONGODB_URI,
|
||||||
|
PORT
|
||||||
|
}
|
||||||
11
part4/bloglist/utils/logger.js
Normal file
11
part4/bloglist/utils/logger.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const info = (...params) => {
|
||||||
|
console.log(...params)
|
||||||
|
}
|
||||||
|
|
||||||
|
const error = (...params) => {
|
||||||
|
console.error(...params)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
info, error
|
||||||
|
}
|
||||||
31
part4/bloglist/utils/middleware.js
Normal file
31
part4/bloglist/utils/middleware.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
const logger = require('./logger')
|
||||||
|
|
||||||
|
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: 'unknown 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user