Skip to content

Commit

Permalink
hosting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yogendradevil committed Oct 11, 2023
1 parent 5c771bb commit 1eb4355
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,122 @@ app.post("/login", async (req, res) => {
});


//post method
app.post("/stext", (req, res) => {
const akey = req.body.key; // access the 'key' field from the form
const textData = req.body.textData; // access the 'textData' field from the form
const jsonData = JSON.stringify({akey, textData});
console.log(jsonData); // the string to be sent to the API
// fetch('http://127.0.0.1:8080/process-string', {
fetch('https://split-shild-api.onrender.com/process-string', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body:textData // send the input string as JSON in the request body
})
.then(response => response.json()) // convert the response to JSON format
.then(async data => {
console.log(data); // this is your JSON data received from the API
// you can store this JSON data in a variable or use it in any way you want
let out1 = data.output1;
let out2 = data.output2;


// Store data in seta collection
const setAData = {
mail: checkmail,
key: akey,
output1: out1
};
const setADocument = await SetA.create(setAData);

// Store data in setb collection
const setBData = {
mail: checkmail,
key: akey,
output2: out2
};
const setBDocument = await SetB.create(setBData);
// res.send('Data received!');
// res.sendFile(path.join(__dirname, "public", "success.html"));
res.render('success', { akey });
})
.catch(error => {console.error(error)
res.status(400).render('fail', { error, file: 'stext' });

}); // handle any errors that may occur
});

// to handle /getData post data
app.post("/getData", async (req, res) => {
const akey = req.body.key;
let out1;
let out2;

try {
// Find out1 from seta collection
const setaDoc = await SetA.findOne({
$and: [
{ key: akey },
{ mail: checkmail }
]});
if (setaDoc) {
out1 = setaDoc.output1;
// Use out1 as needed
console.log('out1:', out1);
} else {
console.log('No document found in seta collection with the specified key.');
res.status(400).render('fail', { error: 'No document found in seta collection with the specified key.', file: 'getData'});
return;
}

// Find out2 from setb collection
const setbDoc = await SetB.findOne({
$and: [
{ key: akey },
{ mail: checkmail }
]});
if (setbDoc) {
out2 = setbDoc.output2;
// Use out2 as needed
console.log('out2:', out2);
} else {
console.log('No document found in setb collection with the specified key.');
res.status(400).render('fail', { error: 'No document found in seta collection with the specified key.', file: 'getData'});
return;
}
} catch (error) {
console.error('Error occurred:', error);
res.status(500).render('fail', { error , file: 'getData'});
return;
}

const sendingJsonData = JSON.stringify({ input1: out1, input2: out2 });

try {
// const response = await fetch('http://127.0.0.1:8080/process-two-strings', {
const response = await fetch('https://split-shild-api.onrender.com/process-two-strings', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: sendingJsonData
});

if (response.ok) {
const responseData = await response.text();
console.log(responseData);
res.render('fetch', { data: responseData });
} else {
console.error('Error occurred:', response.status);
res.status(400).render('fail', { error: 'Error occurred during API request' , file: 'getData'});
}
} catch (error) {
console.error('Error occurred:', error);
res.status(400).render('fail', { error , file: 'getData'});
}
});



Expand Down

0 comments on commit 1eb4355

Please sign in to comment.