-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
190 lines (158 loc) · 4.77 KB
/
app.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Modules */
const express = require('express')
const app = express();
const http = require('http');
const mongoose = require('mongoose');
const debug = require('debug')('nodeapi:server');
const bodyParser = require('body-parser');
const fs = require('fs')
const path = require('path');
const cookieParser = require('cookie-parser');
// const session = require('express-session');
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/user');
const cronJob = require('cron').CronJob;
require('dotenv').config();
const fileupload = require("express-fileupload");
const cors = require("cors");
app.use(cors());
/* helper */
const helper = require('./lib/helper');
/* Routes */
var news = require('./routes/news');
var category = require('./routes/category');
var author = require('./routes/author');
const { header } = require('express-validator');
/* connect mongodb */
//mongoose.connect('mongodb://localhost/astroapi', { useNewUrlParser: true });
mongoose.connect(process.env.db, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true, useFindAndModify: false });
mongoose.debug = true
mongoose.set('debug', true);
app.use(bodyParser.json({ limit: "500mb" }));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ limit: "500mb", extended: true, parameterLimit: 50000 }))
app.use(express.json());
app.use(fileupload());
// app.use(session({
// key: "userID",
// secret: "session_subscribe",
// resave: false,
// saveUnitialized: false,
// cookie: {
// expires: 60 * 60 * 24
// },
// }));
/**
* Get port from environment and store in Express.
*/
// var port = normalizePort(process.env.PORT || '5254');
var port = process.env.PORT || '5254';
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
app.use(express.static(path.join(__dirname, 'uploads')));
app.use(express.static(path.join(__dirname, 'public')));
/* test */
app.get('/', function (req, res) {
res.send('Hello News Db!');
});
app.use(function (req, res, next) {
const allowedOrigins = [
"http://localhost:3000",
"http://localhost:3001",
"http://3.140.177.16:5254",
"http://192.168.0.104:3000",
"https://reactjs-newsdb.netlify.app",
];
const origin = req.headers.origin;
console.log('origin', origin);
// res.setHeader("Access-Control-Allow-Origin", "*");
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader("Access-Control-Allow-Origin", origin);
} else {
res.setHeader("Access-Control-Allow-Origin", "https://reactjs-newsdb.netlify.app");
// res.setHeader("Access-Control-Allow-Origin", "*");
}
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Allow-Headers", "Origin,Content-Type, token, x-id, Content-Length, X-Requested-With, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});
app.use("/api",authRoutes);
app.use("/api",userRoutes);
app.use('/news', news)
app.use('/category', category)
app.use('/author', author)
// app.use(async (req, res, next) => {
// const error = new Error('Not Found');
// error.status = 404
// next(error)
// })
// app.use((err, req, res, next) => {
// req.status(err.status || 500)
// req.send({
// error: {
// status: err.status || 500,
// message: err.message
// },
// })
// })
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
console.log("running on", bind)
}
module.exports = app;