-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
68 lines (57 loc) · 2.03 KB
/
index.ts
File metadata and controls
68 lines (57 loc) · 2.03 KB
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
import * as express from 'express';
import * as mysql from 'mysql';
import config from './config';
import bodyParser = require("body-parser");
import {Request, Response, NextFunction} from "express";
const logger = require('morgan');
const sslRootCas = require('ssl-root-cas');
const cookieParser = require('cookie-parser');
import proceedPayment from './services/proceed-payment';
import * as http from 'http';
import * as https from 'https';
import * as fs from 'fs';
//SERVER CONFIGURATION
const app = express();
app.use(bodyParser.json());
app.use(cookieParser());
app.use(logger('[:date[clf]] - :remote-addr - :method - :url - :status - :response-time ms'));
app.use((req:Request, res:Response, next:NextFunction)=> {
res.header('Access-Control-Allow-Credentials', "true");
res.header('Access-Control-Allow-Origin', "http://localhost:8100");
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,Access-Control-Allow-Credentials,Authorization');
if (req.method === "OPTIONS"){
res.sendStatus(200);
} else {
next();
}
});
//DATABASE CONFIGURATION
const pool = mysql.createPool({
connectionLimit : 10,
waitForConnections : true,
queueLimit :0,
host: config.databaseHost,
user: config.databaseUser,
password: config.databasePassword,
port:config.databasePort,
database: config.databaseName,
//debug : true,
connectTimeout: 10
});
//ROUTES
app.get('/',(req,res,next) => res.json("..."));
app.post('/proceed-payment',proceedPayment(pool));
//START
http.createServer(app).listen(config.port,()=>{
console.log('server started')
});
if (fs.existsSync('./tls/privkey.pem') && fs.existsSync('./tls/fullchain.pem')) {
sslRootCas.inject();
https.createServer({
key: fs.readFileSync('./tls/privkey.pem'),
cert: fs.readFileSync('./tls/fullchain.pem')
}, app).listen(config.portSSL,()=>{
console.log('server ssl started')
});
}