Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda