I am trying to access a database in my Wix site from an external web app I made. All of my requests to the file’s pathname result in a 404 file not found. I want to know if my URL for the request is correct.
Here is the formatting for the URL I am sending my requests to:
https://www.mysite.com/_functions/get_myAPI
I created the .jsw file by clicking on the Public & Backend button and then creating the file within Backend. Is the URL I am requesting the correct way of accessing this file?
Here is how the code looks in my .jsw file. This is for accessing a table in my database and sending it to an external website:
import { ok , created , notFound , serverError } from ‘wix-http-functions’ ;
import wixData from ‘wix-data’ ;
export async function get_myAPI( request ) {
const options = {
“suppressAuth” : true ,
“suppressHooks” : true
};
**try** {
const results = **await** wixData . query ( "TableName" ). find ( options );
const dataSet = results . items . map ( item => {
**return** {
firstName : item . firstName ,
lastName : item . lastName ,
phone : item . phone ,
email : item . email ,
streetAddress : item . streetAddress ,
city : item . city ,
regionStateProvince : item . regionStateProvince ,
country : item . country ,
postalZipCode : item . postalZipCode ,
longAnswerField : item . paragraphField ,
shortAnswerField : item . shortAnswerField
};
});
const response = ok ({ body : { dataSet } });
response . headers = {
"Content-Type" : "application/json" ,
"Access-Control-Allow-Origin" : "https://example.io" ,
"Access-Control-Allow-Headers" : "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" ,
"Access-Control-Allow-Methods" : "GET, POST, OPTIONS"
};
**return** response ;
} catch ( error ) {
console . error ( error );
const response = serverError ({ body : { error : error . message } });
response . headers = {
"Content-Type" : "application/json" ,
"Access-Control-Allow-Origin" : "https://example.io" ,
"Access-Control-Allow-Headers" : "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" ,
"Access-Control-Allow-Methods" : "GET, POST, OPTIONS"
};
**return** response ;
}
}