-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (52 loc) · 2.56 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
// require is a node js keyword like "import" in typescript
// express library, create running server. can listen to incoming request
const express = require('express');
// app is a instance of express library. We are going to use it to be able to create server and be able to respond http request coming from browser/client side.
const app = express();
// Library used to be able to parse incoming data. Client is going to send us JSON data. body-parse helps us to parse the data from the body. It knows how to parse images, file, etc.
const bodyParser = require('body-parser');
// http library allows us to create http servers
const http = require('http');
const path = require('path');
const cookieParser = require('cookie-parser');
const session = require('express-session');
app.use(cookieParser());
if(process.env.SESSION_SECRET) {
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true }));
} else {
app.use(session({
secret: 'test',
resave: true,
saveUninitialized: true}));
}
var passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());
// Initialize bodyparser. We are turn on the feature to parse json data.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Point static path to dist -- For building -- REMOVE
app.use(express.static(path.join(__dirname, 'dist')));
// CORS - Cross-Origin Resource Sharing
// For security purposes, browser only allowed client side to request data from its own server. CORS is a mechanism that determines whether to block or fulfill requests for restricted resources on a web page from another domain outside the domain from which the resource originated.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
const port = process.env.PORT || '3100';
app.set('port', port);
// Create HTTP server
const server = http.createServer(app);
require('./server/app')(app);
// For Build: Catch all other routes and return the index file -- BUILDING
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
// server.listen(port);
server.listen( port , function() {console.log('Running on ' + app.get('port'));});