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

372
Visualizações
Cannot GET error for Express routing with parameters :

I'm getting a Cannot GET error when I try express routing with parameters for the first time and I don't know why.

It worked fine and I installed lodash and now nothing works anymore.

Here`s the code:

    app.get("/posts/:postName"), function (req, res) {

  let requestedTitle = req.params.postName;

    posts.forEach(function(post) {

      let storedTitle = post.title;

      if (storedTitle === requestedTitle) {
        console.log('match found');
      } else {
        console.log('no match found');
      }
    });
  };

I'm using Localhost and when I try to run it it gives me a Cannot Get error.

For example I enter: http://localhost:3000/posts/test And usually it kept me on the home route and console logged if it matched one of my entries before or not.

I have Express installed and everything heres the complete code. But it just won't work anymore after installing lodash, do you guys see an error somewhere here? I'm staring at this for 3 days now lol:

    //jshint esversion:6

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");

const _ = require('lodash');

const homeStartingContent = "text";

const app = express();

let posts = [];



app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));


app.get("/", function(req, res) {
    res.render(__dirname + "/views/home.ejs", {homeStartingContent: homeStartingContent, posts: posts});





});


app.get('/about', (req, res) => {
    res.render('about', {aboutContent: aboutContent});
   });

   app.get('/contact', (req, res) => {
       res.render('contact', {contactContent: contactContent});
      });

      app.get('/compose', (req, res) => {
          res.render('compose');
         });

 app.post('/compose', (req, res) => {


   posts.push(req.body);

console.log(posts)


res.redirect('/')


});




app.get("/posts/:postName"), function (req, res) {

  let requestedTitle = req.params.postName;

    posts.forEach(function(post) {

      let storedTitle = post.title;

      if (storedTitle === requestedTitle) {
        console.log('match found');
      } else {
        console.log('no match found');
      }
    });
  };










app.listen(3000, function() {
  console.log("Server started on port 3000");
});

In the Express documentation it showed this code example and I feel my code should definitely work. I don't understand why it is not.

    app.get('/users/:userId/books/:bookId', (req, res) => {
  res.send(req.params)
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Notice the closing parentheses:

app.get("/posts/:postName"), function (req, res) {
                          ^

It shouldn't be there, but at the end of the handler function.

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