2017-12-02 62 views
1

我正在製作一個nodejs api,其中我從db中獲取申請人的所有記錄。我的API網址是這樣Nodejs api 404在訪問url paremter時未發現錯誤

var express = require('express'); 
var morgan = require('morgan'); 
var bodyParser = require('body-parser'); 
enter code here 
var ApplicantCtrl = require('./controllers/applicantController'); 
var app = express(); 
var port = process.env.Port || 8000; 
app.use(bodyParser.urlencoded({ 
extended: true 
})); 
app.use(bodyParser.json()); 
app.get('api/applicant/:applicantId/getFullDetails',   
    ApplicantCtrl.getApplicantAllData); 
app.listen(port, function() { 
console.log('Server is running on port : ' + port); 
}); 

和applicantController.js代碼是在這裏

var connection = require('./../config'); 
var helpers = require('../helpers/helper'); 


module.exports.getApplicantAllData = function(req , res) { 
var id = req.params.applicantId; 
console.log('in applicantallData'); 
helpers.getApplicantFullData(id) 
    .then((data)=>{ 
     if(data == null){ 
      res.send({ 
       meta : {status : 400, message : 'There is some error with query'} 
      }); 
     } 
     else{ 
      res.send({ 
       meta : {status : 200, message : 'Success'}, 
       data : data 
      }); 
     } 
    }) 
    .catch(function(err){ 
     res.send({ 
      meta : {status : 500, message : 'Internal Server Error'} 
     }); 
    }); 

    } 

但是API響應是這樣

Cannot GET /api/applicant/23/getFullDetails 404 

任何一個可以告訴我,什麼是錯的嗎?爲什麼api響應是404找到的。?

回答

0

看起來您似乎缺少設置服務器路由的快速應用程序中的許多必需代碼。我認爲這就是「在此輸入代碼」部分的用處。

Here is a quick tutorial設置一個非常基本的快遞服務器。

對於您的示例中的網址,您將需要一個路線是這樣的:

router.get('/api/applicant/:id/getFullDetails', function(req, res) { 
    // implementation here 
}); 

另外,無關但更平安,你可能要稍微改變網址是這樣的:/api/applicants/:id/fullDetails

+0

我沒有提到我的server.js文件的完整代碼。我正在更新我的代碼。請參閱更新後的文章並相應地指導我。 –

+0

其中是您正在安裝的代碼的一部分'ApplicantCtrl'?那部分仍然缺失?你應該擁有像Mikelax提到的東西。 app.use('/ api/applicant /:id/getFullDetails',ApplicantCtrl); –

+0

@ J.D。我在我的文章中提到了我的AppicantCtrl代碼。並在我的帖子的第一部分也是ApplicantCtrl安裝代碼,就像這樣 var ApplicantCtrl = require('./ controllers/applicantController'); –

相關問題