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

247
Vistas
why similar routes is not working in express.js example Get ,post ,patch ,delete

Can any one help me only one route is working after adding second route

I am unable to trigger the second route getting error

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot GET /api/sellProduct1</pre>
</body>

</html>

Note : Please don't suggest to change the route names , I am curious why they are not working with same routes but I am using different methods like post,get,delete. plz can any one can help with proper answer.

var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
var productModel = require("./productSchema");

// Connecting to database
var query = 'mongodb://127.0.0.1:27017/mylibs'
const db = (query);
mongoose.Promise = global.Promise;

mongoose.connect(db, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}, function (error) {
    if (error) {
        console.log("Error!" + error);
    }
});

router.get("/", (req, res) => {
    res.send("welcome");
});

// find product by productname
router.get("/sellProduct:product", function (req, res) {

    productModel.findOne({ productName: req.query.product },
        function (err, data) {
            if (err) {
                console.log(err);
            }
            else {
                res.send(data);
            }
        });
});


router.post('/sellProduct', function (req, res) {
    var newProduct = new productModel({
        productName: req.body.productName,
        costPrice: req.body.costPrice,
        sellPrice: 0
    });

    newProduct.save(function (err, data) {
        if (err) {
            console.log(error);
        }
        else {
            res.send("Data inserted");
        }
    });
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should access the routing this way:

/api/sellProduct/1
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