Node Router .delete function returns 404 Not Found?

Issue

I am learning Node/Angular and am having a problem. I am testing using PostMan to send HTTP requests to my API, and am handling DELETE requests like so in a Node routes file called api.js

api.js

var User = require('../models/User');
var config = require('../../config');
var jwt = require('jsonwebtoken');
var bodyParser = require('body-parser');

module.exports = function(app, express) {

var apiRouter = express.Router();

apiRouter.use(function(req, res, next) {
    //Validate users access token on each request to our API.

    var token = req.body.token || req.params.token || req.headers['x-access-token'];

    if (token) {
        jwt.verify(token,config.tokenKey, function(err, decoded) {
            if (err) {
                return res.status(403).send({ success: false, message: 'Authorization required.'});
            } else {
                req.decoded = decoded;
                next();
            }
        });
    } else {
        res.status(403).send({ success: false, message: 'No token provided.'});
        next();
    }

});

apiRouter.get('/users/me', function(req, res) {
    return res.send(req.decoded);
});

apiRouter.post('/users/register', function(req, res) {


    var user = new User;
    user.name = req.body.name;
    user.username = req.body.username;
    user.password = req.body.password;

    user.save(function(err) {
        if (err) {
            return res.send(err);
        } else {
            //User saved!
            return res.json({ message: 'User created', id: user.id });
        }

    });

});

apiRouter.get('/users', function(req, res) {
    User.find(function(err, users) {
        return res.send(users);
    });
});

//READ
apiRouter.get('/users/:user_id', function(req, res) {
    User.findById(req.params.user_id, function(err, user) {
        if (err) return res.send(err);
        if (user) return res.send('Welcome ' + user.username);
        if (!user) return res.send('Invalid User');
    });
});

//UPDATE
apiRouter.put('users/:user_id', function(req, res) {

    User.findById(req.params.user_id, function(err, user) {
        if (err) res.send(err);
        if (req.body.name) user.name = req.body.name;
        if (req.body.username) user.username = req.body.username;
        if (req.body.password) user.password = req.body.password;

        user.save(function(err) {
            if (err) return res.send(err);
            return res.send('User updated');
        });

    });
});

//DELETE (not working???)
apiRouter.delete('users/:user_id', function(req, res) {

    User.Remove({ id: req.params.user_id }, function(err) {
        if (!err) {
            return res.send('User deleted!');
        } else {
            return res.send('Error deleting user!');
        }
    });

});

return apiRouter;

};

My apiRouter.get and apiRouter.post functions are working fine when passing in a valid user id, but sending a request using DELETE is not working and returning this in PostMan when I test

“Cannot DELETE /api/users/55b0dc599e94910c1d37ffa7”

Also I am passing in a valid token as required by my routes middleware.

Anyone know why my DELETE requests are not working but other ones (such as getting a single user) are working just fine?

Solution

I think you are missing parameter: / in route:

apiRouter.delete('/users/:user_id', function(req, res) {

    User.Remove({ id: req.params.user_id }, function(err) {
        if (!err) {
            return res.send('User deleted!');
        } else {
            return res.send('Error deleting user!');
        }
    });

});

Answered By – nitesh singh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published