-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
64 lines (55 loc) · 1.71 KB
/
server.ts
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
// authentication modified from https://scotch.io/tutorials/easy-node-authentication-setup-and-local
require('dotenv').config();
import * as express from 'express';
import * as mongoose from 'mongoose';
import * as bcrypt from 'bcrypt-nodejs';
import * as morgan from 'morgan';
import * as bodyParser from 'body-parser';
import * as helmet from 'helmet';
import * as compression from 'compression';
import * as jwt from 'jsonwebtoken';
import * as cors from 'cors';
import * as passport from 'passport';
import * as passportJWT from 'passport-jwt';
import * as http from 'http';
import * as path from 'path';
import apiRouter from './server/routes/index';
import { User } from './server/models/user';
// const User = require('./server/models/user')(mongoose, bcrypt);
/**
* Instantiate express app
*/
const app = express();
const ExtractJwt = passportJWT.ExtractJwt;
const JWTStrategy = passportJWT.Strategy;
/**
* Database instance
*/
const configDB = require('./config/database');
mongoose.connect(configDB.url, { useMongoClient: true });
/**
* Middleware Setup
*/
app.use(helmet());
app.use(cors());
app.options('*', cors()); // enable pre-flight request for DELETE request
app.use(compression());
app.use(passport.initialize());
app.use(morgan('combined'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
/**
* Api Routes
*/
app.use('/api', apiRouter);
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
/**
* Server
*/
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`API running on localhost:${port}`));