-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (81 loc) · 2.8 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
82
83
84
85
86
87
88
89
90
91
92
93
const express = require("express");
const path = require('path');
const pdf = require('html-pdf');
// const mongoose = require('mongoose');
// const database = require("./config/database");
// // mongoose setup
// mongoose.connect(database.db, { useNewUrlParser: true, useUnifiedTopology: true });
// mongoose.connection.on('connected', () => {
// console.log("connected to database " + database.db);
// })
// mongoose.connection.on('error', (err) => {
// console.log("database error: " + err);
// })
const bodyParser = require("body-parser");
// models
// const userModel = require("./model/user.model");
// const todoModel = require("./model/todo.model");
// controllers
// const todoController = require("./controllers/todoController");
// const userController = require("./controllers/userController");
let app = express();
// setup view template engine
app.set("view engine", "ejs");
// setup static files
app.use(express.static("./public"));
// body parser setup
const urlencodedBodyParser = bodyParser.urlencoded({ extended: false })
const jsonBodyParser = bodyParser.json();
app.use(urlencodedBodyParser);
app.use(jsonBodyParser);
// app.use(expressSession({ secret: 'prem0501', saveUninitialized: false, resave: true }));
// app.get('/ionic', function (req, res) {
// res.sendFile(__dirname + '/public/ionic/index.html');
// });
// app.get('/react', function (req, res) {
// res.sendFile(__dirname + '/public/react/index.html');
// });
app.get('/memo-pdf', (req, res) => {
const query = req.query;
if (!req.query || !req.query.hasOwnProperty('data')) {
res.send(`Data is missing`)
}
try {
const json = JSON.parse(Buffer.from(query.data, 'base64').toString())
if (!json.id) {
res.send(`Data is missing`)
}
// console.log(query.data, json);
res.render('memo.ejs', { data: json }, (err, html) => {
console.log(err);
if (err)
res.send(`Data is missing/incorrect--`)
const options = {
format: 'A4',
orientation: 'portrait',
paginationOffset: 1,
base: 'file://' + __dirname + '/public/assets/',
zoomFactor: "0.5"
};
pdf
.create(html, options)
.toFile(`./public/assets/pdf/Memo_${json.customerDetails.displayName.split(' ').join('_')}.pdf`, (err, fileInfo) => {
console.log(err, fileInfo);
res.sendFile(fileInfo.filename);
})
})
}
catch (e) {
console.log(e);
res.send(`Data is missing/incorrect`)
}
})
// instantiate models
// userModel(mongoose);
// todoModel(mongoose);
const port = process.env.PORT || 4000;
app.listen(port);
console.log("Server is listening on port " + port);
// fire controllers
// todoController(app, { urlencoded: urlencodedBodyParser, json: jsonBodyParser });
// userController(app, { urlencoded: urlencodedBodyParser, json: jsonBodyParser });