-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (140 loc) · 4.92 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import morgan, { format } from 'morgan';
import mongoose from 'mongoose';
import nodemailer from 'nodemailer'
import pdf from 'html-pdf';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import puppeteer from 'puppeteer';
// import routes
import profileRoutes from './routes/Profile.js';
import userRoutes from './routes/User.js';
import recordRoutes from './routes/Record.js';
import clientRoutes from './routes/Client.js';
import pdfTemplate from './Templates/record.js';
import emailTemplate from './Templates/email.js';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// --- Creating a express App -----
const app = express();
// --- Middleware -----
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(morgan('common'));
// --- conneting to mongodb -----
mongoose.connect(process.env.DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
// --- Routes -----
app.use('/profiles', profileRoutes);// edit profile routes
app.use('/users', userRoutes);// authentication routes -> signup, login, forgot password
app.use('/clients', clientRoutes);// client routes
app.use('/records', recordRoutes);// record routes
//Nodemailer for pdf sending
const transporter = nodemailer.createTransport({
port: process.env.SMTP_PORT,
host: process.env.SMTP_HOST,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD
}
});
// const options = {
// format: 'A4',
// }
// sending pdf via email
app.post('/send-pdf', async (req, res) => {
const { email, company } = req.body;
try {
// pdf.create(pdfTemplate(req.body), options).toFile('record.pdf', (err) => {
// if (err) return console.log(err);
// launch a new chrome instance
const browser = await puppeteer.launch({
headless: true
})
// create a new page
const page = await browser.newPage()
// set your html as the pages content
const html = pdfTemplate(req.body);
// console.log("type of html -> ", typeof(html))
// console.log("html page -> ", html)
await page.setContent(html, {
waitUntil: 'domcontentloaded'
})
// or a .pdf file
await page.pdf({
format: 'A4',
path: `${__dirname}/record.pdf`
})
const mailOptions = {
from: `${company?.buisnessName ? company?.buisnessName : "apnaKhata.netlify.app"} <[email protected]>`,// sender address
to: `${email}`,// list of receivers
replyTo: `${company?.email}`,
subject: ` Record bill from ${company?.buisnessName ? company?.buisnessName : "apnakhata.netlify.app"}`,// Subject line
text: ` Record bill from ${company?.buisnessName ? company?.buisnessName : "apnakhata.netlify.app"}`,// plain text body
html: emailTemplate(req.body),
attachments: [
{
filename: 'record.pdf',
path: `${__dirname}/record.pdf`
}
]
}
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.log(err);
}
console.log('Message sent: %s', info);
})
res.status(200).json({ message: 'Email sent' });
// close the browser
await browser.close()
} catch (err) {
console.log(err);
res.status(500).json(`Internal Server Error - ${err}`)
}
})
// creating a pdf
app.post('/create-pdf', async (req, res) => {
try {
// launch a new chrome instance
const browser = await puppeteer.launch({
headless: true
})
// create a new page
const page = await browser.newPage()
// set your html as the pages content
const html = pdfTemplate(req.body);
// console.log("type of html -> ", typeof(html))
// console.log("html page -> ", html)
await page.setContent(html, {
waitUntil: 'domcontentloaded'
})
// or a .pdf file
await page.pdf({
format: 'A4',
path: `${__dirname}/record.pdf`
})
res.json(Promise.resolve())
// close the browser
await browser.close()
} catch (err) {
console.log(err);
res.status(500).json(`Internal Server Error - ${err}`)
}
})
app.get('/get-pdf', (req, res) => {
res.sendFile(`${__dirname}/record.pdf`);
})
// --------- Starting a server -----------
const port = process.env.PORT || 8000;
app.get('/', (req, res) => {
res.send('root route of the app');
})
app.listen(port, (req, res) => {
console.log('server is running on port ' + port);
})