(server-side)
const express = require('express');
//instance of an app
const app = express();
//middleware
const bodyParser = require('body-parser');
app.use(express.urlencoded({extended: false}));
app.use(express.json());
//core package to let the server and client side talk without any security boundaries
const cors = require('cors');
app.use(cors());
app.use(express.static('node'));
//setting up server
const port = 8000;
const server = app.listen(port, listenning);
function listenning(){
console.log(`running on local server: ${port}`);
};
//GET route
const data = [];
app.post('/addMovie', addMovie)
function addMovie(req, res){
data.push(req.body);
console.log(data);
};
(client-side)
const postData = async(URL='', data= {})=>{
console.log(data);
const response = await fetch(url, {
method: 'post',
credentials: 'same-origin',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data),
});
try{
const newData = await response.json();
console.log(newData);
return newData
}catch(error){
console.log("error", error);
}
}
postData('/addMovie', {movie: 'Dark Knight', score: 5});
I'm making a simple POST request via node.js and express but can't find what is wrong, here is the code, when i load the page it says "cannot GET" and gives that error " 404 (Not Found)" can anyone help ??