2017-12-02 66 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'); –

相关问题