[Fixed] NodeJS / Express: rounter.delete shows "not found"

Issue

I am using NodeJS with Express and Mongoose. All the other operations in CRUD run ok and without issues but on router.delete I am getting an error. It seems like express can recognize this.

rounters/

router.delete('/:udid', function (req, res, next) {
    UDLeads.remove({udid: req.params.udid}, function (err, result) {
        if (err) {
            return next(
                {
                    "isSuccess" : false,
                    "message": err,
                    "response": null
                }
            );
        } else {
            res.status(204).json(
                {
                    "isSuccess" : true,
                    "message": null,
                    "response": result
                }
            )
        }
    });
});

Postman call:

enter image description here

And then as a result I get this:
enter image description here

The udid value exists and is true and valid.

Suggestions are very appreciated…

Solution

const express = require('express');
const bodyParser = require('body-parser');
const url = require('url');
const querystring = require('querystring');
const Article = require('./models').Article;

let app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Function to handle the root path
app.delete('/', function (req, res, next) {
    UDLeads.remove({udid: req.query.udid}, function (err, result) {
        if (err) {
            return next(
                {
                    "isSuccess" : false,
                    "message": err,
                    "response": null
                }
            );
        } else {
            res.status(204).json(
                {
                    "isSuccess" : true,
                    "message": null,
                    "response": result
                }
            )
        }
    });
});

let server = app.listen(8080, function() {
    console.log('Server is listening on port 8080')
});

Pass udid as URL params like

localhost:8000/****/lead/222TTT

or read as req.query.udid

localhost:8000/****/lead?udid=222TTT

Leave a Reply

(*) Required, Your email will not be published