So i am working with an api that requires it to be backend called instead of front end called so i have a nodeJs server file called server.js
Right right under my main src
folder i have my server.js file
-/ main folder
--/ src
--/ public
--/node_modules
--/ ..other react files like env and package
--/ server.js
this is a form and as the form progresses i have a file called userData.js that gets updated with user response in a json format.
example: Someone presses they live in a single home then the userData.js updates like this
export const userData = {
'home_type': 'single_home',
'form_type': 'Neptune D2C Form'
}
When the form gets submitted i have that function like this:
const submitForm = () => {
fetch('http://localhost:5000/post-form', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
})
.then((response) => response.json())
.then((data) => {
console.log('Success:', data);
navigate('/submit');
});
}
And in my server.js
file the code looks like this:
app.post('/post-form', async (req, res) => {
const userData = require('./src/data/userData.js');
const neptuneData = require('./src/data/neptuneData.js')
const url = 'https://uat-api.neptuneflood.com/api/v4/rater/quotes';
const token = userData.token;
try {
const response = await axios.post(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: neptuneData
});
res.json(response.data);
console.log('result', response);
}
catch (error) {
res.json({ error: error.toString() });
}
})
I also have another file called neptuneData.js
where i am getting userData.js
object and putting them where they belong inside neptuneData.js, neptudeData is where i have the API body parameters like this
EXAMPLE:
export const neptuneData = {
"isDirectToConsumer": false,
"application": {
"addr1": userData.address,
"city": userData.city,
}
}
When i try to submit the form i get an error, i have tried to change the way i import userData and neptuneData inside server js, i have tried to add module.exports to userData and neptuneData and things dont seem to work. how can i fix this?
#Errors i am getting
import { userData } from "./userData"
^^^^^^
SyntaxError: Cannot use import statement outside a module
export const userData = {
^^^^^^