I'm trying to learn about HTTP Post requests, here's my code:
Client Side:
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
fetch('/api', options).then(response =>{
console.log(response);
});
Server-Side:
const express = require('express');
const app = express();
app.listen(3000, () => console.log('listening at 3000'));
app.use(express.json({limit: '1mb' }));
app.use(express.static('public'));
app.post('/api', (request, response) => {
console.log('I got a request!');
console.log(request.body);
const data = request.body;
response.json({
status: "success",
latitude: data.lat,
longitude: data.lng
});
});
It leads to the following error on my localhost console:
ERR_ABORTED 405 (Method Not Allowed)
And it is complaining about the "fetch" line on the client side code. So there is an issue with the post. Does anyone know what the problem is?