-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
169 lines (116 loc) · 3.22 KB
/
Copy pathserver.js
File metadata and controls
169 lines (116 loc) · 3.22 KB
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
166
167
168
169
const express = require("express");
const app = express();
const path = require("path");
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const Post = require('./model/model');
require('dotenv').config();
const PORT = process.env.PORT;
const { auth, requiresAuth } = require('express-openid-connect');
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
mongoose.connect(
process.env.MONGODB_URI,
{
useNewUrlParser: true,
}
)
.then(() => console.log('MongoDB connected'))
.catch(err => console.log(err.message));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use("/uploads", express.static("uploads"));
const config = {
authRequired: false,
auth0Logout: true,
secret: process.env.SECRET,
baseURL: process.env.BASEURL,
clientID: process.env.CLIENTID,
issuerBaseURL: process.env.ISSUER
};
// auth router attaches /login, /logout, and /callback routes to the baseURL
app.use(auth(config));
app.get('/profilesddd', (req, res) => {
res.json({
isAuthenticated: req.oidc.isAuthenticated(),
user: req.oidc.user,
});
});
app.get('/profilesdd', requiresAuth(), (req, res) => {
res.json(req.oidc.user, null, 2);
});
app.get('/posting', (req, res) => {
res.sendFile(__dirname + "/public/posting.html");
});
app.get('/manage', (req, res) => {
res.sendFile(__dirname + "/public/manage.html");
});
/*
global.img;
app.post("/upload", async (req, res) => {
try {
img = req.body;
}
catch (err) {
console.error(err.message);
res.status(400).send("error");
}
});
*/
//create a post
app.post("/post", (req, res) => {
try {
const post = new Post({
userID: req.oidc.user.sub,
price: req.body.price,
make: req.body.make,
model: req.body.model,
location: req.body.location,
mileage: req.body.mileage,
year: req.body.year,
description: req.body.description,
title: req.body.title,
contact: req.body.contact,
image: req.body.image
});
post.save().then(post => res.status(201).send(post));
} catch (err) {
console.error(err.message);
res.status(400).send("error");
}
});
//get all posts
app.get("/posts", async (req, res) => {
try {
const posts = await Post.find();
//const posts = await Post.find().sort({ createdAt: -1 });
res.status(200).json(posts);
} catch (err) {
console.error(err.message);
res.status(400).send("error");
}
});
//get all of a user's posts
app.get("/userposts", async (req, res) => {
try {
const posts = await Post.find({userID: req.oidc.user.sub});
res.status(200).json(posts);
} catch (err) {
console.error(err.message);
res.status(400).send("error");
}
});
//delete
app.delete("/delete/:id", async (req, res) => {
try {
const id = req.params.id;
const post = await Post.findByIdAndDelete(id);
res.status(200).json(post);
} catch (err) {
res.status(400).json({ msg: err.message })
res.status(400).send("error");
}
});
app.listen(PORT, () => {
console.log("server running on port");
});