2017-01-13 1 views
0

나는 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,210

user.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"} 

답변

0

각도 오류 스택 추적은 매우 분명하다.
Express 라우터에서 POST 쿼리에 대해이 특정 끝점을 처리하지 마십시오.

따라서 서버는 오류을 던집니다. 이 같은

시도 뭔가 : 특급 (및 NodeJS)와

router 
    .post('/api/users', function (req, res) { 
     // do what you want with your user here 
    }) 

명시 적으로 서버 자원에 대한 각각의 액세스를 지정해야합니다.

사이드 노트에서 보듯이 애플리케이션 로직을 유지 관리하기 쉽게 유지하려고합니다.

+0

경로에서'app.use'를 사용하고 있습니다. 이미 index.js에 router.post가 있습니다. 다시 URL이 필요한 이유는 무엇입니까? – hussain

+0

이것이 작동하지 않기 때문에 ... 내가 제안한 것을 시도 했습니까? 라우터가 작동 할 때 코드를 모듈화 할 수 있습니다. – TGrif

관련 문제