Why am I getting a 404 error when using ngresource save?

63 views Asked by At

I'm following an old Pluralsight tutorial that is no longer updated and have hit a wall: I am using Visual Studio 2013 and Angular JS v1.4, and have to use this tutorial as we are using this version of AngularJS at work.

HTML TEMLPLATE: NewEvent.html

<div style="padding-left:20px; padding-right: 20px">
<div class="container">
    <h1>New Event</h1>
    <hr />
    <form name="newEventForm">
        <fieldset>
            <label for="eventName">Event Name:</label>
            <input id="eventName" type="text" required ng-model="event.name" placeholder="Name of your event..." />
            <label for="eventDate">Event Date:</label>
            <input id="eventDate" type="text" required ng-pattern="/\d\d/\d\d/\d\d\d\d/" ng-model="event.date" placeholder="format (mm/dd/yyyy)..." />
            <label for="eventTime">Event Time:</label>
            <input id="eventTime" type="text" required ng-model="event.time" placeholder="Start and end time..." />
            <label for="eventLocation">Event Location:</label>
            <input id="eventLocation" type="text" required ng-model="event.location.address" placeholder="Address of event..." />
            <br />
            <input id="eventCity" type="text" required ng-model="event.location.city" class="input-small" placeholder="City..." />
            <input id="eventProvince" type="text" ng-model="event.location.province" class="input-small" placeholder="Province..." />
            <div>{{event.name}}</div>
            <label for="eventImageUrl">Image:</label>
            <input id="eventImageUrl" type="url" ng-model="event.imageUrl" class="input-xlarge" placeholder="Url of image..." />
        </fieldset>

        <img ng-src="{{event.imageUrl}}" src="" />
        <br />
        <br />

        <button type="submit" ng-disabled="newEventForm.$invalid" ng-click="saveEvent(event, newEventForm)" class="btn btn-primary">Save</button>
        <button type="button" ng-click="cancelEdit()" class="btn btn-default">Cancel</button>
    </form>
</div>

CONTROLLER: EditEventController.js

'use strict'

eventsApp.controller('EditEventController',
    function EditEventController($scope, eventData) {

    $scope.event = {};

    $scope.saveEvent = function (event, newEventForm) {
        if (newEventForm.$valid) {
            eventData.save(event)
                .$promise
                .then(function (response) { console.log('success', response) })
                .catch(function (response) { console.log('failure', response) });
        }
    };

    $scope.cancelEdit = function () {
        window.location = "./EventDetails.html";
    }
});

SERVICE: EventData.js

eventsApp.factory('eventData', function ($resource) {
    var resource = $resource('data/event/:id.json', { id: '@id' }, { "getAll": { method: "GET", isArray: true, params: { something: "foo" } } });
    return {
        getEvent: function () {
            return resource.get({ id: 1 });
        },
        save: function (event) {
            event.id = 999;
            return resource.save(event);
        }
    }
});

I had to specify '.json' in the var resource line and that's the only change I made to the code given by the tutors, because another page (which uses the getEvent function in the service) wouldn't work unless I did that.

When I click save, I get the 'failure' response in the console even though the model for the event has built correctly.

Screenshot of error in console:-

Screenshot of error in console

File structure:-

File structure

The 404 error appears to be attached to the save destination, even though as far as I can tell the URL is correct. How do I correctly use ngresource.save to save this file?

1

There are 1 answers

0
codeskraps On

web-server.js - check get & post path

const express = require('express');
const path = require('path');
const events = require('./eventsController');
const app = express();
const rootPath = path.normalize(__dirname + '/../');
const bodyParser = require('body-parser');
const port = 8000;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(rootPath + '/app'));

app.get('/data/event/:id', events.get);
app.post('/data/event/:id', events.save);

app.listen(port, function () {
    console.log('Listening on port ' + port + '...');
});

EventData.js - check at the resource path

'use strict';

eventsApp.factory('eventData', function ($resource) {
    var resource = $resource('/data/event/:id', { id: '@id' });
    return {
        getEvent: function () {
            return resource.get({ id: 2 });
        },
        save: function (event) {
            event.id = 999;
            return resource.save(event);
        }
    };
});