forked from coreui/coreui-free-react-admin-template
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
99 lines (95 loc) · 3.64 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
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
//ee0125/index.js
const express = require('express')
const app = express()
const path = require('path')
const cors = require('cors')
const mongoose = require('./backend/routes/Schemas/db')
mongoose.connection.on('open', () => {
console.log('DB on')
const bodyParser = require('body-parser')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
//post, get時的解碼
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", 'http://localhost:3000');
// res.header("Access-Control-Allow-Credentials", true);
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// res.header("Access-Control-Allow-Headers", 'Origin, X-Requested-With, Content-Type, Accept');
// next();
// });
// app.use(
// cors({
// origin: 'http://localhost:3000',
// methods: ['POST', 'PUT', 'GET', 'OPTIONS', 'HEAD', 'PATCH', 'DELETE'],
// credentials: true,
// }),
// )
//session 設定
//參考網站https://www.cnblogs.com/chyingp/p/nodejs-learning-express-session.html
app.use(
session({
name: 'eeplus',
secret: 'fuewhzk', // 用来對session id相關的cookie進行簽名,建議128byte亂碼
//store: new FileStore({ logFn: function () {} }), // 本地儲存session(文本文件,也可以選擇其他store,比如redis的)
store: new MongoStore({
mongooseConnection: mongoose.connection,
}),
saveUninitialized: false, // 是否自动保存未初始化的會話,建議false
resave: false, // 是否每次都重新保存會話,建議false
cookie: {
httpOnly: true, //false前端可read和set
maxAge: 60 * 60 * 1000, // 有效期(ms)
},
}),
)
app.use('/api', require('./backend/routes/api'))
// })
//frontend
if (!process.env.HERO) {
const connectHistoryApiFallback = require('connect-history-api-fallback')
app.use(
connectHistoryApiFallback({
verbose: false,
}),
)
const JSON_DIR = path.join(__dirname, './client/public') // for develop
const DIST_DIR = path.join(__dirname, './client/dist')
const HTML_FILE = path.join(__dirname, './client/dist/index.html')
app.use(express.static(DIST_DIR))
app.use(express.static(JSON_DIR)) // for develop
app.get('/', (req, res) => {
res.sendFile(HTML_FILE) // EDIT
})
}
//old frontend
//Serve static files from the React app
//詳細資訊看:https://expressjs.com/zh-tw/starter/static-files.html
// app.use(express.static(path.join(__dirname, 'client/build')))
/*app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/build/index.html')); //這個缺點是react build的index不是我們寫的那個
//res.redirect('/'); //這個按F5會亂跳,先捨棄
});*/
//server on
app.listen(process.env.PORT || 1993, () => {
if (process.env.HERO) {
const { wakeDyno } = require('heroku-keep-awake')
wakeDyno('https://eeplus-back.herokuapp.com/', {
logging: false,
stopTimes: { start: '16:00', end: '00:00' }, //time zone +0,so -8hr
})
}
console.log(`Server is up on port ${process.env.PORT || 1993}.`)
})
//https server
// connect to https://localhost:1993
// const fs = require('fs')
// const options = {
// key: fs.readFileSync('./certificate.key'),
// cert: fs.readFileSync('./certificate.crt')
// };
// const https = require('https');
// https.createServer(null, app).listen(process.env.PORT||1993, () => {
// console.log(`Server is up on port ${process.env.PORT || 1993}.`)
})