Not sure why I keep getting a 405 error. My post request is getting rejected every time.
SERVER CODE
// getting datastores
const datastore = require('nedb');
const customerRecordsdb = new datastore('CustomerRecords.db');
customerRecordsdb.loadDatabase();
// importing express
const port = 5500;
const express = require('express');
const application = express();
application.listen(port, () => console.log("Listening at " + port));
application.use(express.static('public'));
application.use(express.json( {limit: '1mb'} ));
// CUSTOMER RECORD FUNCTIONS
application.get('/RetrieveCustomerInformation', (request, response) => {
console.log(request, response)
customerRecordsdb.find({}, (error, data) => {
if (error) {
response.end();
return;
}
response.json(data);
})
})
CLIENT CODE
function onFormSubmit() {
let formData = readFormData();
console.log("got here");
if (selectedRow != null) { return }
if (confirm("Are you sure you want to add this record to the database?:")) {
insertNewRecord(formData);
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData)
}
fetch('/RetrieveCustomerInformation', options)
} else {
if (confirm("Are you sure you want to edit this record?:")) {
updateRecord(formData);
}
}
resetForm();
}
DIRECTORY HIERARCHY
https://i.stack.imgur.com/fNSOh.png
ERROR
When I try to submit the post request to the server:
‘customerrecords.js:21 POST http://127.0.0.1:5500/RetrieveCustomerInformation net::ERR_ABORTED 405 (Method Not Allowed)’