-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (51 loc) · 1.42 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
const express = require("express");
const passport = require("passport");
const { port, jwtSecret } = require("./config/config");
const cors = require("cors");
const bodyParser = require("body-parser");
const expressJwt = require("express-jwt");
const User = require("./models/User");
// initialize express application
const app = express();
// connect to database
require("./dbconnect");
// cors
app.use(cors());
// use body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// passport config
require("./config/passport-linkedin");
app.use(passport.initialize());
// auth routes for linkedin
const authLinkedin = require("./routes/authLinkedin");
app.use("/auth/linkedin/", authLinkedin);
// get logged in user
app.get(
"/get-logged-user",
expressJwt({
secret: jwtSecret,
requestProperty: "auth"
}),
async (req, res) => {
try {
let user = await User.findById(req.auth.id);
res.json(user);
} catch (err) {
console.log(err);
res.status(400).json({ error: JSON.stringify(err) });
}
}
);
// Serve static assets if in prod
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// start listening to requests on port
app.listen(port, err => {
if (err) throw err;
console.log(`Listening on port ${port}`);
});