Currently, I am experimenting to determine if a POST request will function correctly on my server through the '/db' route. Surprisingly, while the GET request is successful, the POST request consistently results in a '404 - not found' error.
To illustrate, here is a portion of my JavaScript code:
function getReq() {
let p = fetch("http://localhost:3000/db").then((a)=>a.json()).then((x)=>console.log(x))
}
async function postReq(obj) {
console.log(obj)
const p = await fetch("http://localhost:3000/db",
{
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: obj
}
)
}
This snippet shows my Express code:
const app = express();
app.use(bodyParser.json());
app.get('/db', (req,res)=>
{
res.sendFile("db.json");
});
app.post("/db", function(req,res)
{
res.send("ok");
});
app.use(express.static('public'));
app.listen(3000, (ok) => console.log("Okk"));
Both db.json and app.js exist within the same folder, with only JSON strings being passed into postReq. Additionally, all other files are located in the 'public' directory. I would greatly appreciate some guidance on what might be causing this issue. Thank you in advance.