-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (34 loc) · 1.19 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
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
const path = require("path");
const expressJWT = require('express-jwt');
const app = express();
const config = require("./config/config");
const webRouter = require("./config/webRoutes");
const apiRouter = require("./config/apiRoutes");
mongoose.connect(config.db);
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
app.use(cors());
app.use(express.static(path.join(__dirname, "./public")));
app.use('/api', expressJWT({ secret: config.secret })
.unless({
path: [
{ url: '/api/register', methods: ['POST'] },
{ url: '/api/login', methods: ['POST'] }
]
}));
app.use(jwtErrorHandler);
function jwtErrorHandler (err, req, res, next) {
if (err.name === "UnauthorizedError") {
return res.status(401).json({ message: 'Unathorized request.' });
}
return next();
}
app.use("/", webRouter);
app.use("/api", apiRouter);
app.listen(config.port, () => console.log(`Express started on port: ${config.port}`));