This repository has been archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (60 loc) · 1.92 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
const express = require('express');
const app = express();
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// res.header ("Access-Control-Allow-Credentials", true)
// res.header ("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
// next();
// });
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
})
const bodyParser = require('body-parser');
const path = require('path');
const session = require('express-session');
// Body parser setup
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static(path.join(__dirname, 'public')));
// Session setup
app.use(session({
secret: process.env.PASSPORT_SECRET,
name: 'Catscookie',
proxy: true,
resave: true,
saveUninitialized: true
}));
// route files
const piRoutes = require('./backend/piRoutes');
const auth = require('./backend/auth');
const api = require('./backend/api');
// Rasberry Pi API routes
app.use('/', piRoutes);
app.use('/api', auth);
app.use('/api', api);
app.use('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html'); // For React/Redux
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, error => {
error
? console.error(error)
: console.info(`==> 🌎 Listening on port ${PORT}. Visit http://localhost:${PORT}/ in your browser.`);
});