-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (60 loc) · 1.71 KB
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const subscriberRouter = require('./routes/subscribers');
const Subscriber = require('./models/Subcriber');
const db = mongoose.connection;
db.on('error', (error) => console.log(error));
db.once('open', () => console.log("Connected to database!"));
app.use('subscribers', subscriberRouter);
app.use(express.json());
app.post("/", async (req, res) => {
const subscriber = new Subscriber({
name: req.body.name,
surname: req.body.surname,
address: {
new Address({
req.body.city,
req.body.street,
req.body.number
}),
});
try {
const newSubscriber = await subscriber.save();
res.status(201).json(newSubscriber);
} catch (err) {
res.status(500).json({
message: err.message
});
}
});
app.get("/", async (req, res) => {
try {
const subs = Subcriber.find();
res.status(200).json(subs);
} catch (err) {
res.status(500).json({
message: err.message
});
}
});
app.get("/:id", function(req, res) {
try {
const sub = Subcriber.findById(req.params.id);
res.status(200).json(sub);
} catch (err) {
res.status(500).json({
message: err.message
});
}
});
app.listen(3000, () => {
console.log("Server has started on port 3000");
});
async function getSubscriber(req, res, next) {
try {
subscriber = await Subscriber.findById(req.params.id);
} catch (err) {
res.status(500).json();
}
}