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

243
Views
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 answers
Answer question

0

You should access the routing this way:

/api/sellProduct/1
about 4 years ago · Juan Pablo Isaza 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!