11const express = require ( "express" ) ;
2- const fs = require ( "fs " ) ;
3- const nodemailer = require ( "nodemailer" ) ;
2+ var nodemailer = require ( "nodemailer " ) ;
3+ require ( "dotenv" ) . config ( ) ;
44const { OpenAI } = require ( "openai" ) ;
55const app = express ( ) ;
66const bodyParser = require ( "body-parser" ) ;
7- const path = require ( "path" ) ;
8- require ( "dotenv" ) . config ( ) ;
9- let doubleResponse ;
10-
117app . use ( bodyParser . json ( ) ) ;
128app . post ( "/server.js" , ( req , res ) => {
139 const dishCountry = req . body . recipe_country_of_origin ;
@@ -19,6 +15,9 @@ app.post("/server.js", (req, res) => {
1915const openai = new OpenAI ( {
2016 apiKey : process . env . openaiAPI ,
2117} ) ;
18+
19+ let doubleResponse ;
20+
2221app . get ( "/email" , async ( req , res ) => {
2322 var transporter = nodemailer . createTransport ( {
2423 service : process . env . service ,
@@ -28,49 +27,16 @@ app.get("/email", async (req, res) => {
2827 } ,
2928 } ) ;
3029
31- const folderPath = path . join ( __dirname , "./public/url_folder" ) ;
32- const url = doubleResponse . image . data [ 0 ] . url ;
33- const filePath = path . join ( folderPath , "url_folder.txt" ) ;
34- fs . writeFileSync ( filePath , url ) ;
35-
36- const emailDocument = `
37- <html>
38- <head>
39- <style>
40- .preserve-line-breaks {
41- white-space: pre-line
42- }
43- .user-img {
44- width: 200px;
45- height: 200px;
46- }
47- </style>
48- </head>
49- <body class="preserve-line-breaks" >
50- ${ doubleResponse . text . choices [ 0 ] . message . content }
51- <br />
52- Embedded image:
53- <br />
54- <img class="user-img" src="${ url } "/>
55- </body>
56- </html>
57- ` ;
58-
59- var mailOptions = {
60- from : process . env . from ,
61- to : req . query . user_email_address ,
62- subject : "Your recipe from recipe-for-success dynamic app" ,
63- html : emailDocument ,
64- attachments : [
65- {
66- filename : "url_folder.txt" ,
67- path : path . join ( __dirname , "/public/url_folder/url_folder.txt" ) ,
68- cid : "url" ,
69- } ,
70- ] ,
71- } ;
72-
73- console . log ( mailOptions )
30+ if ( doubleResponse && doubleResponse . text && doubleResponse . text . choices ) {
31+ var mailOptions = {
32+ from : process . env . from ,
33+ to : req . query . user_email_address ,
34+ subject : "Your recipe from recipe-for-success dynamic app" ,
35+ text : doubleResponse . text . choices [ 0 ] . message . content ,
36+ } ;
37+ } else {
38+ console . log ( "doubleResponse is not defined yet." ) ;
39+ }
7440
7541 transporter . sendMail ( mailOptions , function ( error , info ) {
7642 if ( error ) {
@@ -91,20 +57,19 @@ app.get("/openai", async (req, res) => {
9157 {
9258 user_otherdietary_requirements :
9359 req . query . what_are_user_other_dietary_requirements ,
94- } ,
95- { I_do_not_eat : req . query . I_do_not_eat }
60+ }
9661 ) ;
9762
9863 const prompt = `Provide a recipe for a dish from ${
9964 req . query . recipe_country_of_origin
10065 } , taking into account the fact that I'm ${
10166 req . query . is_lactose_intolerant === "true"
102- ? "lactose intolerant, "
103- : "not lactose intolerant, "
104- } ${ req . query . is_vegan === "true" ? "I'm vegan" : "I'm not vegan" } and ${
67+ ? "lactose intolerant"
68+ : "not lactose intolerant"
69+ } ${ req . query . is_vegan === "true" ? "vegan" : "not vegan" } and ${
10570 req . query . what_are_user_other_dietary_requirements === ""
10671 ? "I have no other dietary requirements"
107- : ` ${ req . query . I_do_not_eat } ${ req . query . what_are_user_other_dietary_requirements } `
72+ : req . query . what_are_user_other_dietary_requirements
10873 } `;
10974
11075 console . log ( prompt ) ;
@@ -130,7 +95,6 @@ app.get("/openai", async (req, res) => {
13095 text : completion ,
13196 image : imageResponse ,
13297 } ;
133- console . log ( doubleResponse . text . choices ) ;
13498 res . json ( doubleResponse ) ;
13599 } catch ( error ) {
136100 console . error ( "An error occurred:" , error . message ) ;
0 commit comments