-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
139 lines (120 loc) · 3.18 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
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const userRoutes = require('./routes/user')
const jwt = require('jsonwebtoken')
const db = require('./models')
const port = process.env.PORT || 3000
// Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'))
app.use(express.static('views'))
// routes for login and signup
app.use('/user', userRoutes);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
})
app.get('/home', (req, res) => {
res.sendFile(__dirname + '/views/home.html');
})
app.get('/home', (req, res) => {
res.sendFile(__dirname + '/views/news.html');
})
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/ipinfo.html');
})
app.post('/verify', verifyToken, (req, res) => {
let verified= jwt.verify(req.token, 'waffles')
console.log("verified: ", verified)
res.json(verified)
})
// SAMPLE PROTECTED ROUTE!
// protected route - a route only a user with a jwt token in their header can access.
app.post('/protectedPage', verifyToken, (req, res) => {
console.log(req.token)
jwt.verify(req.token, 'waffles', (err, authData) => {
if(err) {
res.sendStatus(403);
} else {
res.json({
message: 'Post created',
authData
});
}
});
});
// FORMAT OF TOKEN
// Authorization: Bearer <access_token>
// Verify Tokenj
function verifyToken(req, res, next) {
console.log("in verify...");
// Get auth header value
// when we send our token, we want to send it in our header
const bearerHeader = req.headers['authorization'];
console.log(bearerHeader)
// Check if bearer is undefined
if(typeof bearerHeader !== 'undefined'){
const bearer = bearerHeader.split(' ');
// Get token from array
const bearerToken = bearer[1];
// Set the token
req.token = bearerToken;
// Next middleware
next();
} else {
// Forbidden
res.sendStatus(403);
}
}
app.post("/quote", (req, res) => {
let quote = req.body
console.log(quote);
db.Quote.create(quote, (err,newQuote)=>{
console.log(newQuote);
if(err){
res.json(err)
}
res.json({newQuote})
})
});
app.get("/quote/:id", (req, res) => {
db.Quote.find({user: req.params.id}).exec((err,quotes)=>{
console.log(quotes);
if(err){
res.json(err)
}
res.json(quotes)
})
});
////
app.delete("/quote/:id", (req, res) => {
db.Quote.remove({_id: req.params.id}).exec((err, quotes) => {
if(err){
res.json(err)
}
res.json(quotes)
})
});
////
// delete food //
app.delete('/api/foods/:id', (req, res) => {
let foodId = req.params.id;
db.Food.deleteOne(
{ _id: foodId },
(err, deletedFood) => {
if(err) { return console.log(err) }
res.json(deletedFood);
});
});
////
app.listen(port)
// app.listen(port, () => console.log(`Server listening on port ${port}!`)) //port=3000
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(express.static('views'));
app.get('/', (req,res) => {
res.sendFile('views/index.html', {root: __dirname});
});
app.use('/user', userRoutes);