-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (75 loc) · 2.61 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
var express=require('express');
var bodyParser=require('body-parser');
var morgan=require('morgan');
var mongoose=require('mongoose');
var app=express();
var router =express.Router();
var appRoutes=require('./app/routes/api')(router);
var port = process.env.PORT || 8080;
var path=require('path');
var multer=require('multer');
var fs = require('fs');
var cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: 'shaastra-apps',
api_key: process.env.API_KEY,
api_secret: process.env.SECRET
});
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname+'/public')); //allow front end folder 'public' access to all the back end stuff
app.use('/api',appRoutes); //not conflict with angular routes
var storage = multer.diskStorage({
destination: function(req,file, cb){
cb(null,'./app/routes/uploads/'); //storing images temporarily
},
filename: function(req,file, cb){
if(!file.originalname.match(/\.(pdf)$/)){
var err = new Error();
err.code='filetype';
return cb(err);
} else {
cb(null, file.originalname);
}
}
});
var upload = multer({
storage: storage,
limits: {fileSize: 10000000}
}).single('image'); //image is the name given to the formdata
app.post('/upload',function(req,res){
var username=req.body.username;
upload(req,res,function(err){
if(err){
if(err.code === 'LIMIT_FILE_SIZE'){
res.json({success: false, message: "File limit exceeded (max: 10MB)"});
}
else if(err.code === 'filetype'){
res.json({success: false, message: "Only pdf files are supported"});
}else {
console.log(err);
res.json({success: false, message: "Unable to upload"});
}
}
else {
if(req.file){
cloudinary.uploader.upload("./app/routes/uploads/" + req.file.originalname,
function(result) {
var filePath = './app/routes/uploads/' + req.file.originalname;
fs.unlink(filePath);
res.json({success: true, message: "File uploaded", url: result.secure_url});
}
);
}else {
res.json({success: false, message: "No file received"});
}
}
});
});
app.get('*',function(req,res){
res.sendFile(path.join(__dirname + '/public/app/views/index.html'));
});
app.listen(port,'10.160.0.2',function(){
console.log("Server running on port 8080");
});