/
index.js
105 lines (66 loc) · 1.84 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
100
101
102
103
104
105
import http from 'http';
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import multer from 'multer'
import path from 'path';
import {connect} from "./database";
import AppRouter from './router'
import nodemailer from 'nodemailer'
import {smtp, s3Config, s3Region,s3Bucket} from './config'
// Amazon S3 Setup
import AWS from 'aws-sdk'
import multerS3 from 'multer-s3'
AWS.config.update(s3Config);
AWS.config.region = s3Region ;
const s3 = new AWS.S3();
// Setup Email
let email = nodemailer.createTransport(smtp);
// File storage config
const storageDir = path.join(__dirname, '..', 'storage');
//const upload = multer({ storage: storageConfig }); // local upload.
const upload = multer({
storage: multerS3({
s3: s3,
bucket: s3Bucket,
metadata: function (req, file, cb) {
cb(null, {fieldName: file.fieldname});
},
key: function (req, file, cb) {
const filename = `${Date.now().toString()}-${file.originalname}`;
cb(null, filename)
}
})
})
// End file storage config
const PORT = 3000;
const app = express();
app.server = http.createServer(app);
app.use(morgan('dev'));
app.use(cors({
exposedHeaders: "*"
}));
app.use(bodyParser.json({
limit: '50mb'
}));
app.set('root', __dirname);
app.set('storageDir', storageDir);
app.upload = upload;
app.email = email;
app.s3 = s3;
//Connect to the database.
connect((err, db) => {
if(err){
console.log("An error connecting to the database", err);
throw (err);
}
app.db = db;
app.set('db', db);
// init routers.
new AppRouter(app);
app.server.listen(process.env.PORT || PORT, () => {
console.log(`App is running on port ${app.server.address().port}`);
});
});
export default app;