AngularJS $资源writeFileSync给出错误:ENOENT:没有这样的文件或目录,打开

时间:2016-07-16 20:17:18

标签: javascript angularjs

我一直在做一个关于AngularJS的教程,我一直试图填写这个表单,并使用$ resource服务将其作为json文件保存到磁盘。

我一直收到错误

Error: ENOENT: no such file or directory, open '/data/event/999.json'

应用程序/ NewEvent.html

<!doctype html>
<html lang="en" ng-app="eventsApp">
<head>
    <meta charset="utf-8">
    <title>Event Registration</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/app.css">
</head>
<body>
<div class="container">
    <div class="navbar-inner">
        <ul class="nav">
            <li><a href="/NewEvent.html">Create Event</a></li>
        </ul>
    </div>

    <style> input.ng-invalid.ng-dirty {background-color:pink}</style>

    <div ng-controller="EditEventController" 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" ng-model="event.time" placeholder="start and end time ...">
                    <label for="eventLocation"> Event Location</label>
                    <input id="eventLocation" type="text"  ng-model="event.location.address" placeholder="Address of event...">
                    <br>
                    <input  id="eventCity" type=text" 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...">
                    <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>
    </div>
</div>

<script src="/lib/jquery.min.js"></script>
<script src="/lib/underscore-1.4.4.min.js"></script>
<script src="/lib/bootstrap.min.js"></script>
<script src="/lib/angular/angular.js"></script>
<script src="/lib/angular/angular-resource.js"></script>
<script src="/lib/angular/angular-sanitize.js"></script>
<script src="/js/app.js"></script>
<script src="/js/controllers/EventController.js"></script>
<script src="/js/controllers/EditEventController.js"></script>
<script src="/js/services/EventData.js"></script>
<script src="/js/filters.js"></script>

</body>

</html>

应用程序/ JS /控制器/ EditEventController.js

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


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

    }

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




})

应用程序/ JS /服务/ EventData.js

eventsApp.factory('eventData', function ($resource) {

    return  {
        getEvent: function() {
           return $resource('/data/event/:id.json',{id:'@id'}).get({id:1});
        },
        save: function (event) {
            event.id = 999;
            return $resource('/data/event/:id',{id:'@id'}).save(event);
        }
    }
})

脚本/ eventsController.js

var fs = require('fs')
module.exports.get = function (req,res) {
    var event = fs.readFileSync('/data/event/' + req.params.id ,'utf8');
    res.setHeader('Content-Type','application/json');
    res.send(event)
}

module.exports.save = function (req,res) {
    var event = req.body;
    fs.writeFileSync('/data/event/' + req.params.id +'.json', JSON.stringify(event));
    res.send(event)
}

脚本和应用程序处于同一级别 EventDetails.html位于app内,与NewEvent.html

处于同一级别

1 个答案:

答案 0 :(得分:0)

乍一看,代码看起来是正确的,这可能是一个权限问题吗?