-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
67 lines (54 loc) · 1.52 KB
/
app.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
const express = require('express');
const mongoose = require('mongoose');
const multer = require('multer');
const cors = require('cors');
const fs = require('fs');
const { ConnectDB } = require('./Middlewares/db');
const { SetRoutes } = require('./Controllers');
require('dotenv').config();
const PORT = process.env.PORT || 8000;
const MongoURL = process.env.MONGOURL;
const app = express();
// make 'files' folder if not exist for storing uploaded files
if(!fs.existsSync('files')){
fs.mkdirSync('files');
}
try {
ConnectDB(MongoURL);
} catch (next) {}
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({extended: true}));
// multer setting for file storgae location and name
const fileStorage = multer.diskStorage({
destination: (req, file, cb) =>{
cb(null, 'files');
},
filename: (req, file, cb) => {
cb(null, req.body.uname + "_" + file.originalname);
}
});
app.use(multer({storage: fileStorage}).single('source'))
/**testing in browser*/
// const path = require('path');
// app.use(express.static(path.join(__dirname, "public")));
// app.get("/", (req, res) =>{
// res.render('index.html');
// });
// app.post("/file", (req, res, next) => {
// const fileInfo = req.file;
// console.log(fileInfo);
// });
/** */
SetRoutes(app); //Add REST API endpoints
//Default error handler
app.use((err, req, res, next) => {
console.log(err);
res.status(400).send({
success: false,
message: err.message || err
});
});
app.listen(PORT, ()=>{
console.log(`Server listening on ${PORT}`);
})