Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Can anyone tell me whats wrong here in this simple code?

(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 ??

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Error 404 means the URL is wrong.

You pass '/addMovie' as URL, and use it for fetch.

You might want to update this:

const response = await fetch(url, {

to this

const response = await fetch('http://localhost:8000' + url, {

By adding 'http://localhost:8000' as prefix of your url.

about 4 years ago · Juan Pablo Isaza Denunciar

0

In your case you should POST to the endpoint:

http://localhost:8000/addMovie
about 4 years ago · Juan Pablo Isaza Denunciar

0

@Balastrong's answer is the most correct (you need to use http://localhost:8000 as your base URI), but you'll also need to update your parameter name for your postData function. Right now URL is capitalized, but your use of the variable later on is all lower case.

So use const postData = async(url='',

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda