2016-11-07 4 views
0

평균 스택에 대한 소개로, 내 로컬의 github에서 this 예제를 수행하고있었습니다. 이것은 하나의 테이블 (또는 문서?)과 함께 매우 기본적인 연락처 목록 CRUD 응용 프로그램입니다.평균 스택 앱 내의 mongodb에서 "404를 찾을 수 없음"얻기

가이드를 따라 내 로컬에 mongodb를 성공적으로 설치했습니다.

server.js :

var express = require('express'); 
var app = express(); 
var mongojs = require('mongojs'); 
var db = mongojs('contactlist', ['contactlist']); 
var bodyParser = require('body-parser'); 

app.use(express.static(__dirname + '/public')); 
app.use(bodyParser.json()); 

app.get('/contactlist', function (req, res) { 
    console.log('I received a GET request'); 

    db.contactlist.find(function (err, docs) { 
    console.log(docs); 
    res.json(docs); 
    }); 
}); 

app.post('/contactlist', function (req, res) { 
    console.log(req.body); 
    db.contactlist.insert(req.body, function(err, doc) { 
    res.json(doc); 
    }); 
}); 

app.delete('/contactlist/:id', function (req, res) { 
    var id = req.params.id; 
    console.log(id); 
    db.contactlist.remove({_id: mongojs.ObjectId(id)}, function (err, doc) { 
    res.json(doc); 
    }); 
}); 

app.get('/contactlist/:id', function (req, res) { 
    var id = req.params.id; 
    console.log(id); 
    db.contactlist.findOne({_id: mongojs.ObjectId(id)}, function (err, doc) { 
    res.json(doc); 
    }); 
}); 

app.put('/contactlist/:id', function (req, res) { 
    var id = req.params.id; 
    console.log(req.body.name); 
    db.contactlist.findAndModify({ 
    query: {_id: mongojs.ObjectId(id)}, 
    update: {$set: {name: req.body.name, email: req.body.email, number: req.body.number}}, 
    new: true}, function (err, doc) { 
     res.json(doc); 
    } 
); 
}); 

app.listen(3000); 
console.log("Server running on port 3000"); 

공공/컨트롤러/controller.js :

var myApp = angular.module('myApp', []); 
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) { 
    console.log("Hello World from controller"); 


var refresh = function() { 
    $http.get('/contactlist').success(function(response) { 
    console.log("I got the data I requested"); 
    $scope.contactlist = response; 
    $scope.contact = ""; 
    }); 
}; 

refresh(); 

$scope.addContact = function() { 
    console.log($scope.contact); 
    $http.post('/contactlist', $scope.contact).success(function(response) { 
    console.log(response); 
    refresh(); 
    }); 
}; 

$scope.remove = function(id) { 
    console.log(id); 
    $http.delete('/contactlist/' + id).success(function(response) { 
    refresh(); 
    }); 
}; 

$scope.edit = function(id) { 
    console.log(id); 
    $http.get('/contactlist/' + id).success(function(response) { 
    $scope.contact = response; 
    }); 
}; 

$scope.update = function() { 
    console.log($scope.contact._id); 
    $http.put('/contactlist/' + $scope.contact._id, $scope.contact).success(function(response) { 
    refresh(); 
    }) 
}; 

$scope.deselect = function() { 
    $scope.contact = ""; 
} 

}]); 

공공/index.html을 :

<!DOCTYPE> 
<html ng-app="myApp"> 
<head> 
<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> 

<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css"> 

    <title>Contact List App</title> 
</head> 
<body> 
    <div class="container" ng-controller="AppCtrl"> 
    <h1>Contact List App</h1> 

    <table class="table"> 
     <thead> 
     <tr> 
      <th>Name</th>   
      <th>Email</th> 
      <th>Number</th> 
      <th>Action</th> 
      <th>&nbsp;</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td><input class="form-control" ng-model="contact.name"></td> 
      <td><input class="form-control" ng-model="contact.email"></td> 
      <td><input class="form-control" ng-model="contact.number"></td> 
      <td><button class="btn btn-primary" ng-click="addContact()">Add Contact</button></td> 
      <td><button class="btn btn-info" ng-click="update()">Update</button>&nbsp;&nbsp;<button class="btn btn-info" ng-click="deselect()">Clear</button></td> 
     </tr> 
     <tr ng-repeat="contact in contactlist"> 
      <td>{{contact.name}}</td> 
      <td>{{contact.email}}</td> 
      <td>{{contact.number}}</td> 
      <td><button class="btn btn-danger" ng-click="remove(contact._id)">Remove</button></td> 
      <td><button class="btn btn-warning" ng-click="edit(contact._id)">Edit</button></td> 
     </tr> 
     </tbody> 
    </table> 

    </div> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script> 
<script src="controllers/controller.js"></script> 
</body> 
</html> 

package.json :

{ 
    "name": "contactlistapp", 
    "version": "1.0.0", 
    "description": "", 
    "main": "server.js", 
    "dependencies": { 
    "body-parser": "^1.10.2", 
    "express": "^4.11.1", 
    "mongojs": "^0.18.1" 
    }, 
    "devDependencies": {}, 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1", 
    "start": "node server.js" 
    }, 
    "repository": { 
    "type": "git", 
    "url": "https://github.com/michaelcheng429/meanstacktutorial.git" 
    }, 
    "author": "", 
    "license": "ISC", 
    "bugs": { 
    "url": "https://github.com/michaelcheng429/meanstacktutorial/issues" 
    }, 
    "homepage": "https://github.com/michaelcheng429/meanstacktutorial" 
} 
0 나는 몽고 명령 행에 쓴

스크립트 (윈도우 7) :

use contactlist 
db.contactlist.insert({name: 'Tom', email: '[email protected]', number: '(444)444-4444'}) 

크롬 콘솔에 오류 내가 index.html을 엽니 다

GET http://localhost:63342/contactlist 404 (Not Found)(anonymous function) @ angular.js:9827m @ angular.js:9628f @ angular.js:9344(anonymous function) @ angular.js:13189$eval @ angular.js:14401$digest @ angular.js:14217$apply @ angular.js:14506(anonymous function) @ angular.js:1448e @ angular.js:4185d @ angular.js:1446sc @ angular.js:1466Jd @ angular.js:1360(anonymous function) @ angular.js:26125a @ angular.js:2744c @ angular.js:3014 
controller.js:19 Object {name: "sa", email: "sa", number: "as"}email: "sa"name: "sa"number: "as"__proto__: Object 
+0

는'contactList'은 =', @str 죄송합니다 – str

+0

가 변화하는 것을 잊었다 contactlist' 게시물을 편집 :

당신은 요청에 도메인을 추가하여 고칠 수 있습니다. 이전 오류 메시지였습니다. – brainmassage

+0

http : // localhost : 63342/contactlist, 포트 3000에서 앱을 실행 중일 때 – krakig

답변

1

귀하의 각 응용 프로그램에 의해 제공되지 않는 귀하의 node.js 서버는 즉, $http.get('/contactlist') 번으로 전화하면 각도 앱으로 동일한 도메인에서 localhost:63342이라는 경로를 호출한다는 의미입니다. ! $http.get('localhost:3000/contactlist')

+0

이제 CORS 오류 – brainmassage

+0

가 발생했습니다. 다른 도메인에서 호출되는 경로 때문입니다. CORS를 허용하려면이 스레드를보십시오. http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-node-js – krakig