master
Karolin Rafalski 7 years ago
parent a9a456cbb4
commit 3d632682d5

@ -1,3 +1,4 @@
const cors = require('cors')
const express = require('express') const express = require('express')
const holidays = express.Router() const holidays = express.Router()
const Holiday = require('../models/holidays.js') const Holiday = require('../models/holidays.js')
@ -5,13 +6,15 @@ const Holiday = require('../models/holidays.js')
holidays.post('/', async (req, res) => { holidays.post('/', async (req, res) => {
Holiday.create(req.body, (error, createdHoliday) => { Holiday.create(req.body, (error, createdHoliday) => {
if (error) { if (error) {
res.status(400).json({ error: error.message }) console.log(error)
} res.status(418).json({ error: error })
} else {
res.status(200).send(createdHoliday) // .json() will send proper headers in response so client knows it's json coming back res.status(200).send(createdHoliday) // .json() will send proper headers in response so client knows it's json coming back
}
}) })
}) })
holidays.get('/', (req, res) => { holidays.get('/', cors(), (req, res) => {
Holiday.find({}, (err, foundHolidays) => { Holiday.find({}, (err, foundHolidays) => {
if (err) { if (err) {
res.status(400).json({ error: err.message }) res.status(400).json({ error: err.message })

@ -1,11 +1,11 @@
const mongoose = require('mongoose') const mongoose = require('mongoose')
const holidaySchema = mongoose.Schema({ const holidaySchema = mongoose.Schema({
name: {type: String, required: true}, name: { type: String, required: true },
celebrated: {type: Boolean, default: false}, celebrated: { type: Boolean, default: false },
description: {type: String, default: 'Best holiday ever!'}, description: { type: String, default: 'Best holiday ever!' },
likes: {type: Number, default: 0}, likes: { type: Number, default: 0 },
tags: [{type: String}] tags: [{ type: String }]
}) })
module.exports = mongoose.model('Holiday', holidaySchema) module.exports = mongoose.model('Holiday', holidaySchema)

@ -6,11 +6,22 @@ const mongoose = require('mongoose')
require('dotenv').config() require('dotenv').config()
const app = express() const app = express()
const PORT = process.env.PORT const PORT = process.env.PORT
const MONGODB_URI = process.env.MONGODB_URI const MONGODB_URI = process.env.MONGODB_URI + '/merncrud'
const whitelist = ['http://localhost:3000', 'https://fathomless-sierra-68956.herokuapp.com']
const corsOptions = {
origin (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
// middleware // middleware
app.use(express.json()) // use .json(), not .urlencoded() app.use(express.json()) // use .json(), not .urlencoded()
app.use(cors()) app.use(cors(corsOptions))
// Error / Disconnection // Error / Disconnection
mongoose.connection.on('error', err => console.log(err.message + ' is Mongod not running?')) mongoose.connection.on('error', err => console.log(err.message + ' is Mongod not running?'))

Loading…
Cancel
Save