• Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Precios
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

130
Vistas
Unexplainable Node JS "Cannot POST/GET" errors

I have an NodeJS SPA with a strange behaviour and for that I MUST to use "brute force" to make express.static() works:

app.use('*/images', express.static(path.join(__dirname + '/public/images')));

Now I'm facing a problem in another URL that I must to make a POST req:

(GoogleChrome imagem)

On the red dot: Failed to load resource: the server responded with a status of 404 ()

That´s the server.js file:

// Load Node modules
var express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const ejs = require('ejs');
var falaUser = require('./routes/falaUser');

(...)

// Render static files
app.use(bodyParser.json());
app.use('*/images', express.static(path.join(__dirname + '/public/images')));
app.use('*/js', express.static(path.join(__dirname + '/public/js')));
app.use('*/css', express.static(path.join(__dirname + '/public/css')));
app.use('*/slick-1.8.1', express.static(path.join(__dirname + '/public/slick-1.8.1')));
app.use('*/falauser', falaUser);


(...)

// Root Route
app.get('/strapiClient/', function (req, res) {
    res.render('pages/index');
});

That´s the falaUser.js route file:

const express = require('express');
const router = express.Router();
const axios = require('axios');
var config = require("config");

router.post('/strapiClient', function (req, res, next) {
  console.log(req.body);
  axios
    .post(config.get("botUrl"),
      req.body
    )
    .then((dataBot) => {
      console.log(dataBot.data.responses);
      res.json({
        "responses": dataBot.data.responses
      });
    })
    .catch((error) => {
      console.error(error);
      res.status(500).json(error);
    })

});

The output from .catch(error) is SyntaxError: Unexpected token < in JSON at position 0

To be sure that the issue is due routing, I tested with a GET requisition, putting this code on falaUser.js:

router.get('/strapiClient', function (req, res) {
  res.json({
    fruta: 'lichia'
  });
});

And I had a "Cannot GET /strapiClient/falauser" error...

Any clue on how to dig into it?

The page is here, if want to take a look: go to the botton of the page and type "opa" in the chatbot area.

9 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

app.use("/path1", router.post("/path2", ...))

matches only POST /path1/path2 requests. In your case, path2 = "strapiClient", hence the POST URL must end with /strapiClient, and POST /strapiClient/falauser is not matched.

See the following sentence in the Router documentation:

The “mount” path is stripped and is not visible to the middleware function. The main effect of this feature is that a mounted middleware function may operate without code changes regardless of its “prefix” pathname.

9 months 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 empleo Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.