File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1
1
const express = require ( 'express' ) ;
2
2
const connectToMongo = require ( './db' ) ;
3
3
const cors = require ( 'cors' ) ;
4
+ const handleError = require ( './middleware/error' ) ;
4
5
5
6
const load = async ( ) => {
6
7
await connectToMongo ( ) ;
@@ -10,6 +11,7 @@ const load = async () => {
10
11
11
12
app . use ( express . json ( ) ) ;
12
13
app . use ( cors ( ) ) ;
14
+
13
15
14
16
// Available Routes
15
17
app . use ( '/api/auth' , require ( './routes/auth' ) ) ;
@@ -20,6 +22,9 @@ const load = async () => {
20
22
res . send ( 'Hi!' ) ;
21
23
} ) ;
22
24
25
+ // Error handling middleware
26
+ app . use ( handleError ) ;
27
+
23
28
app . listen ( PORT , ( ) => {
24
29
console . log ( `The App is running at http://localhost:${ PORT } ` ) ;
25
30
} ) ;
Original file line number Diff line number Diff line change
1
+ const nodemailer = require ( 'nodemailer' ) ;
2
+ const outlookEmail = process . env . outlookEmail ;
3
+ const outlookPassword = process . env . outlookPassword ;
4
+
5
+ const handleError = ( err , req , res , next ) => {
6
+ console . error ( err . stack ) ;
7
+ const statusCode = err . statusCode || 500 ;
8
+ const message = err . message || 'Internal Server Error' ;
9
+
10
+ console . log ( "Send message" )
11
+
12
+ if ( statusCode === 500 ) {
13
+ const transporter = nodemailer . createTransport ( {
14
+ service : 'hotmail' ,
15
+ auth : {
16
+ user : outlookEmail ,
17
+ pass : outlookPassword
18
+ }
19
+ } ) ;
20
+
21
+ const mailOptions = {
22
+ from : outlookEmail ,
23
+ to : outlookEmail ,
24
+ subject : `Important!! : ${ message } ` ,
25
+ text : `Stack Trace:\n${ err . stack } `
26
+ } ;
27
+
28
+ transporter . sendMail ( mailOptions , ( error , info ) => {
29
+ if ( error ) {
30
+ console . error ( error ) ;
31
+ } else {
32
+ console . log ( `Email sent: ${ info . response } ` ) ;
33
+ }
34
+ } ) ;
35
+ }
36
+ res . status ( statusCode ) . send ( message ) ;
37
+ }
38
+
39
+ module . exports = handleError ;
You can’t perform that action at this time.
0 commit comments