-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·81 lines (66 loc) · 2.14 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//printserver
var https = require('https');
var fs = require('fs');
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var jwt = require('jwt-simple');
var pdfkit = require('pdfkit');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
const secret = 'cps3232';
//connect to database
mongoose.connect("mongodb://printserver:1234@imgserver:27018/usrImagesDB");
console.log("Connected to usrImagesDB as user:printserver on port 27018");
//define schema; ie the format the data in the database will take
var imgschema = new mongoose.Schema({
username:String,
imgIds:[String]
});
//define data model based on schema
//Create collection in db if doesn't exist
var users = mongoose.model('users',imgschema);
//modules.export = users;
////////////////////
const app = express();
app.use(urlencodedParser);
app.set('view engine','ejs');
// app.use('/auth',authRoutes);
//first parameter tells which url format directory is to be mounted for
//__dirname + ... is the name of the directory on the server.
//for all urls ending in
app.use('/gallery',express.static(__dirname+'/images'));
app.use('/',express.static(__dirname));
app.get('/',function(req,res){
res.render('auth');
});
app.get('/logout',function(req,res){
res.render('auth_exp');
});
// app.get('/home',function(req,res){
// res.render('home');
// });
app.get('/gallery/:token',function(req,res){//token will be username encrypted with secret
var decoded = jwt.decode(req.params.token,secret);
console.log(decoded);
if(decoded.exp < Date.now()){
res.render("auth_exp");
}
users.findOne({username:decoded.username},
function(err,user){
if(!user){
console.log("user not found");
res.end("user not found");
}
else{
console.log(user);
res.render('images',{username:user.username,imgIds:user.imgIds/*,rows:rows*/});
}
});
});
var credentials = {
ca: fs.readFileSync('ca-chain.cert.pem'),
key: fs.readFileSync('printserver.key.pem'),
cert: fs.readFileSync('printserver.cert.pem'),
passphrase: 'cthulhu'
};
https.createServer(credentials,app).listen(443);