-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (62 loc) · 1.81 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
const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const helmet = require('helmet');
require('dotenv/config');
const app = express();
const port = process.env.PORT || 5000;
const { headlines, randWords, test2 } = require('./cron/cron');
// cron jobs
headlines;
randWords;
test2;
//Middlewares
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(helmet());
const CORS_ORIGIN = process.env.CORS_ORIGIN;
app.use(
cors({
origin: `${CORS_ORIGIN}`,
credentials: true,
})
);
app.use(morgan('common'));
//Import Routes
const usersRoute = require('./routes/users');
const refreshTokenRoute = require('./routes/refresh_token');
const listsRoute = require('./routes/lists');
const votesRoute = require('./routes/votes');
const commentsRoute = require('./routes/comments');
const randomRoute = require('./routes/random');
//Route Middlewares
app.use('/user', usersRoute);
app.use('/refresh_token', cookieParser());
app.use('/refresh_token', refreshTokenRoute);
app.use('/lists', listsRoute);
app.use('/votes', votesRoute);
app.use('/comments', commentsRoute);
app.use('/random', randomRoute);
app.use('/test', (req, res) => {
res.send('got to test 1');
});
//ROUTES
app.get('/', (req, res) => {
res.send('gimme five');
});
app.use((req, res, next) => {
const error = new Error(`Not Found - ${req.originalUrl}`);
res.status(404);
next(error);
});
app.use((error, req, res, next) => {
const statusCode = res.statusCode === 200 ? 500 : res.statusCode;
res.status(statusCode);
res.json({
message: error.message,
stack: process.env.NODE_ENV === 'production' ? '🥞' : error.stack,
});
});
//Where we listen for server
app.listen(port, () => console.log(`server listening on port ${port}!`));