Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

490
Views
No se puede OBTENER el error para el enrutamiento Express con parámetros:

Recibo un error Cannot GET cuando intento el enrutamiento rápido con parámetros por primera vez y no sé por qué.

Funcionó bien e instalé lodash y ahora ya nada funciona.

Aquí está el código:

 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'); } }); };

Estoy usando Localhost y cuando trato de ejecutarlo me da un error Cannot Get.

Por ejemplo, ingreso: http://localhost:3000/posts/test Y, por lo general, me mantuvo en la ruta de inicio y la consola registró si coincidía con una de mis entradas anteriores o no.

Tengo instalado Express y todo aquí está el código completo. Pero ya no funcionará después de instalar lodash, ¿ven un error en alguna parte aquí? Estoy mirando esto durante 3 días jajaja:

 //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"); });

En la documentación de Express, mostró este ejemplo de código y creo que mi código definitivamente debería funcionar. No entiendo por qué no lo es.

 app.get('/users/:userId/books/:bookId', (req, res) => { res.send(req.params) })
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Observe los paréntesis de cierre:

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

No debería estar allí, sino al final de la función del controlador.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!