나는 client factory에서 인쇄하고있는 firstname과 lastname을 포함하고있는 객체를 가지고있어서 백엔드 개발에 익숙하지 않으므로 사용자 스키마와 api를 작성하여 데이터를 게시했지만 404 오류가 발생합니다. 아래에서 언급 한 오류입니다. 어떤 아이디어가 잘못 구현 되었습니까?Mean stack을 사용하여 mongo collection에 데이터를 게시하는 방법은 무엇입니까?
clientFactory.js
create : function(userData) {
console.log('factory',userData);
return $http.post('/api/users', userData);
}
서버 코드
응용 프로그램> routes.js 응용 프로그램> API에서
module.exports = function(app) {
app.use('api/users', require('./api/user'));
app.get('*', function(req, res) {
res.sendfile('./public/views/index.html'); // load our public/index.html file
// res.sendFile(path.join(__dirname, ''../public/views/index.html''));
});
};
폴더> 사용자
user.index.js
,763,210user.model.js
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
var UserSchema = new mongoose.Schema({
firstName: String,
lastName: String
});
module.exports = mongoose.model('User', UserSchema);
user.controller.js
exports.create = function(req, res) {
console.log(req.body);
User.createAsync(req.body)
.then(responseWithResult(res, 201))
.catch(handleError(res));
}
오류
angular.js:12410 POST http://localhost:8080/api/users 404 (Not Found)
(anonymous) @ angular.js:12410
p @ angular.js:12155
(anonymous) @ angular.js:11908
(anonymous) @ angular.js:16648
$eval @ angular.js:17972
$digest @ angular.js:17786
$apply @ angular.js:18080
(anonymous) @ angular.js:26749
cg @ angular.js:3613
d @ angular.js:3601
angular.js:14328 Possibly unhandled rejection: {"data":"Cannot POST /api/users\n","status":404,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/users","data":{"firstName":"Kun","lastName":"Zhang"},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Not Found"}
경로에서'app.use'를 사용하고 있습니다. 이미 index.js에 router.post가 있습니다. 다시 URL이 필요한 이유는 무엇입니까? – hussain
이것이 작동하지 않기 때문에 ... 내가 제안한 것을 시도 했습니까? 라우터가 작동 할 때 코드를 모듈화 할 수 있습니다. – TGrif