Ok, I read the MEAN Machine book and follow the examples. I am trying to figure out what is wrong with my code, so it will not make a DELETE request. GET, PUT and POST work as they should.
I have this code on my .js server:
app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); res.header('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization'); next(); }); var apiRouter = express.Router(); apiRouter.route('/users/:user_id') .get( function (req, res) { User.findById( req.params.user_id, function (err, user) { if (err) res.send (err); res.json(user); }); }) .put( 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) send (err); res.json({message: 'User updated'}); }); }) .delete( function (req, res) { User.remove({ _id: req.params.user_id }, function (err, user) { if (err) return res.send(err); res.json({ message: 'Deleted' }); }); });
});
I have a set of Modulus MongoDB database users, and when I try to use POSTMAN with localhost: 8080 / api / users / 5610e5576d827dc41fb8e6e, POSTMAN says
Cannot DELETE /api/users/5610e5576d827dc41fb8e6e
while my Node server with Morgan says
DELETE /api/users/5610e5576d827dc41fb8e6e 404
Why am I getting 404? What am I doing wrong?
Full code
Rodmentou
source share