-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (143 loc) · 4.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const entryModel = require("./models/entry");
// data for the application
let data = [
{
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",
},
{
id: 5,
name: "Fiona Poppendieck",
number: "39-23-6423123",
},
];
// initializing the server app
const server = express();
// cors handling
server.use(cors())
// need the following parser that allows easy access to request body that contains the post info
server.use(express.json())
// morgan token to display a POST requests body
morgan.token('body', function(req, res){
return JSON.stringify(req.body)
});
// following logger used to log HTTP requests made to the server
server.use(morgan(':method :url :status :res[content-length] - :response-time ms :body'))
server.use(express.static('build'))
const generateRandomId = () => {
return Math.floor(Math.random() * 10000)
}
// api definitions
server.get("/", (req, res) =>
res.send("<h1>Hello! this is the server speaking...</h1>")
);
server.get("/info", (req, res, next) => {
let timestamp = new Date();
let day = new Intl.DateTimeFormat("en-US", { weekday: "short" }).format(
timestamp
);
let month = new Intl.DateTimeFormat("en-US", { month: "short" }).format(
timestamp
);
let date = timestamp.getDate();
let year = timestamp.getFullYear();
let time = timestamp.toTimeString();
return entryModel.count({})
.then(dbRes => {
res.send( `<div>Phonebook has info for ${dbRes} people</div><div>${day} ${month} ${date} ${year} ${time}</div>` );
})
.catch(err => next(err))
});
server.get("/api/persons", (req, res, next) => {
return entryModel.find({})
.then(dbRes => res.json(dbRes))
.catch(err => {
console.log('Something went wrong while fetching all entries:',err)
next(err)
})
});
server.post("/api/persons", (req, res, next) => {
let body = req.body
if(!body) {
res.status(400).json({ error: 'content missing' })
}
else{
const entry = new entryModel({...body })
return entry.save()
.then(dbRes => res.json(dbRes))
.catch(err => next(err))
}
});
server.get("/api/persons/:id", (req, res, next) => {
const id = req.params.id;
return entryModel.findOne({ _id: id })
.then(dbRes => {
if (dbRes) res.json(dbRes);
else res.status(404).json({ error: `No entry with id- ${id} found` })
})
.catch(err => next(err))
});
server.put("/api/persons/:id", (req, res, next) => {
const id = req.params.id;
let body = req.body;
if(!body) {
res.status(400).json({ error: 'content missing' })
}
else{
return entryModel.findOneAndUpdate({ _id: id }, { "number": body.number }, { runValidators: true })
.then(dbRes => {
console.log(dbRes)
if(dbRes != null ) res.status(200).json({ message: `Successfully updated number for entry with id-${id}`})
else res.status(404).json({ error: `No entry with id- ${id} was found for updation` })
})
.catch(err => next(err))
}
})
server.delete("/api/persons/:id", (req, res, next) => {
const id = req.params.id;
return entryModel.deleteOne({_id: id })
.then(dbRes => {
if(dbRes.deletedCount > 0) res.status(200).json({ message: `Successfully deleted entry for the phonebook.` })
else res.status(404).json({ message: `Entry with id-${id} does not exist in the database` })
})
.catch(err => next(err))
})
// making the server listen to requests on a port
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// error handling middleware
const unknownEndpoint = (req, res) => {
res.status(404).send({ error: 'Invalid endpoint' })
}
server.use(unknownEndpoint)
const errorHandler = (error, req, res, next) => {
if(error.name === 'CastError'){
return res.status(400).json({ error: 'Malformatted id' })
}
else if (error.name === 'ValidationError') {
return res.status(400).json({ error: error.message })
}
next(error)
}
server.use(errorHandler)