Upload part 3
This commit is contained in:
@@ -1,104 +1,105 @@
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const cors = require("cors");
|
||||
const morgan = require("morgan");
|
||||
/* eslint-disable no-unused-vars */
|
||||
require('dotenv').config()
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
const cors = require('cors')
|
||||
const morgan = require('morgan')
|
||||
const Phonebook = require('./models/phonebook')
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cors());
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
app.use(express.static('build'))
|
||||
|
||||
morgan.token("body", function (req, res) {
|
||||
return req.method === "POST" ? JSON.stringify(req.body) : "";
|
||||
});
|
||||
morgan.token('body', function (req, res) {
|
||||
return req.method === 'POST' ? JSON.stringify(req.body) : ''
|
||||
})
|
||||
|
||||
app.use(
|
||||
morgan(":method :url :status :res[content-length] - :response-time ms :body")
|
||||
);
|
||||
morgan(':method :url :status :res[content-length] - :response-time ms :body')
|
||||
)
|
||||
|
||||
let persons = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Arto Hellas",
|
||||
number: "040-123456",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Ada Lovelace",
|
||||
number: "39-44-5323523",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Dan Abramov",
|
||||
number: "12-43-234345",
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "Mary Poppendieck",
|
||||
number: "39-23-6423122",
|
||||
},
|
||||
];
|
||||
app.get('/api/persons', (req, res) => {
|
||||
Phonebook.find({}).then(result => {
|
||||
res.json(result)
|
||||
})
|
||||
})
|
||||
|
||||
app.get("/api/persons", (req, res) => {
|
||||
res.json(persons);
|
||||
});
|
||||
app.get('/info', (req, res) => {
|
||||
Phonebook.find({}).then(result => {
|
||||
res.send(`<p>Phonebook has info for ${result.length} people</p><p>${Date()}</p>`)
|
||||
})
|
||||
})
|
||||
|
||||
app.get("/info", (req, res) => {
|
||||
const date = new Date();
|
||||
res.send(
|
||||
`<p>Phonebook has info for ${persons.length} people</p><p>${date}</p>`
|
||||
);
|
||||
});
|
||||
app.get('/api/persons/:id', (req, res, next) => {
|
||||
Phonebook.findById(req.params.id)
|
||||
.then(result => {
|
||||
if (result) {
|
||||
res.json(result)
|
||||
} else {
|
||||
res.status(404).end()
|
||||
}
|
||||
})
|
||||
.catch(error => next(error))
|
||||
})
|
||||
|
||||
app.get("/api/persons/:id", (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
const person = persons.find((person) => {
|
||||
return person.id === id;
|
||||
});
|
||||
if (person) {
|
||||
res.json(person);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
});
|
||||
app.put('/api/persons/:id', (req, res, next) => {
|
||||
const { name, number } = req.body
|
||||
|
||||
app.delete("/api/persons/:id", (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
persons = persons.filter((person) => person.id !== id);
|
||||
res.status(204).end();
|
||||
});
|
||||
Phonebook.findByIdAndUpdate(
|
||||
req.params.id,
|
||||
{ name, number },
|
||||
{ new: true, runValidators: true, context: 'query' }
|
||||
)
|
||||
.then(updatedPerson => {
|
||||
res.json(updatedPerson)
|
||||
})
|
||||
.catch(error => next(error))
|
||||
})
|
||||
|
||||
const generateId = () => {
|
||||
const maxId = persons.length > 0 ? Math.max(...persons.map((n) => n.id)) : 0;
|
||||
return maxId + 1;
|
||||
};
|
||||
app.delete('/api/persons/:id', (req, res, next) => {
|
||||
Phonebook.findByIdAndRemove(req.params.id)
|
||||
.then(() => {
|
||||
res.status(204).end()
|
||||
})
|
||||
.catch(error => next(error))
|
||||
})
|
||||
|
||||
app.post("/api/persons", (req, res) => {
|
||||
const body = req.body;
|
||||
if (!body.name || !body.number) {
|
||||
app.post('/api/persons', (req, res, next) => {
|
||||
const { name, number } = req.body
|
||||
if ( name || number) {
|
||||
return res.status(400).json({
|
||||
error: "name or number is missing",
|
||||
});
|
||||
} else if (persons.find((person) => person.name === body.name)) {
|
||||
return res.status(400).json({
|
||||
error: "name must be unique",
|
||||
});
|
||||
error: 'name or number is missing',
|
||||
})
|
||||
}
|
||||
const person = {
|
||||
id: generateId(),
|
||||
name: body.name,
|
||||
number: body.number,
|
||||
};
|
||||
persons = persons.concat(person);
|
||||
res.json(person);
|
||||
});
|
||||
const phonebook = new Phonebook({
|
||||
name: name,
|
||||
number: number,
|
||||
})
|
||||
phonebook.save().then(result => {
|
||||
res.json(result)
|
||||
}).catch(error => next(error))
|
||||
})
|
||||
|
||||
const unknownEndpoint = (req, res) => {
|
||||
res.status(404).send({ error: "unknown endpoint" });
|
||||
};
|
||||
res.status(404).send({ error: 'unknown endpoint' })
|
||||
}
|
||||
|
||||
app.use(unknownEndpoint);
|
||||
app.use(unknownEndpoint)
|
||||
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const errorHandler = (error, res, next) => {
|
||||
console.error(error.message)
|
||||
|
||||
if (error.name === 'CastError') {
|
||||
return res.status(400).send({ error: 'malformatted id' })
|
||||
} else if (error.name === 'ValidationError') {
|
||||
return res.status(400).json({ error: error.message })
|
||||
}
|
||||
next(error)
|
||||
}
|
||||
|
||||
app.use(errorHandler)
|
||||
|
||||
const PORT = process.env.PORT
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
console.log(`Server running on port ${PORT}`)
|
||||
})
|
||||
Reference in New Issue
Block a user