Home In angularjs 1.0.8 not works PUT rest service
Reply: 0

In angularjs 1.0.8 not works PUT rest service

user7563 Published in September 19, 2018, 7:12 pm

I have angularjs application which have CRUD Rest service. Create, Read and Delete methods works well, but PUT not works. I found on Stackoverflow and there are the same problems with accepted answers e.g.:

Angular JS: Full example of GET/POST/DELETE/PUT client for a REST/CRUD backend?

AngularJS - PUT method not working (404 error)

So I do it like in this answers but my update (by PUT method) not works (Status code: 404 'Cannot PUT /api/adverts'). Maybe folks notice something in my code. One important thing is that I use angular 1.0.8

services.js (angularjs):

var motoAdsServices = angular.module('motoAdsServices', ['ngResource']);

motoAdsServices.factory('Advert', ['$resource', function($resource) {
    return $resource('/api/adverts/:advertId', {}, {
       update: {method:'PUT', params: {advertId: '@advertId'}}


  <label class="control-label">Brand</label>
  <div class="controls">
    <select name="brand" ng-model="editAdvert.brand" required ng-options="b.name for b in brands">
      <option value=""></option>

  <label class="control-label">Model</label>
  <div class="controls">
    <select name="model" ng-model="editAdvert.model" required ng-options="m.name for m in editAdvert.brand.models">
      <option value=""></option>

controllers.js (angular.js - I removed no significant code):

$scope.updateAdvert = function() {
  var editAdvert = {
    _id: $scope.editAdvert._id,
    brandName: $scope.editAdvert.brand.name,
    modelName: $scope.editAdvert.model.name,
    year: $scope.editAdvert.year,
    price: $scope.editAdvert.price,
    imageUrl: $scope.editAdvert.imageUrl,
    countryName: $scope.editAdvert.country.name,
    regionName: $scope.editAdvert.region.name

  // !!! I TRY THIS TO FIX !!!
  $scope.advertId = $scope.editAdvert._id;

  Advert.update(editAdvert, function() {
    previousAdvert = angular.copy($scope.editAdvert);
    alert('Advert updated');

server.js (nodejs):

var express = require('express');
var path = require('path');
var http = require('http');
var adverts = require('./routes/adverts');

var app = express();

app.configure(function() {
  app.set('port', process.env.PORT || 3000);
  app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */
  app.use(express.static(path.join(__dirname, 'public')));

app.get('/api/adverts', adverts.findAll);
app.get('/api/adverts/:id', adverts.findById);
app.post('/api/adverts', adverts.add);
app.put('/api/adverts/:id', adverts.update);
app.delete('/api/adverts/:id', adverts.remove);

http.createServer(app).listen(app.get('port'), function() {
  console.log("Express server listening on port " + app.get('port'));

In response I get 404 and 'Cannot PUT /api/adverts'. I'm almost convinced that it is the problem of lack '_id' at the end of URL. See the picture:

Please look at my code and give some suggestion.

share|improve this question

1 Answer 1

active oldest votes
up vote 3 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310568 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO