访问url paremter时未发现Nodejs api 404错误

时间:2017-12-02 21:43:17

标签: node.js rest api

我正在创建一个nodejs api,其中我从db获取申请人的所有记录。我的api url就像这样

@RunWith(SpringRunner.class)
@DataJpaTest
@Transactional
public class UserRepositoryTest {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private UserRepository userRepository;

    @Test
    public void whenPersistingAnUserTheUserAndImageAreSaved(){
        UserImage userImage = new UserImage("key","URL");
        User user = new User("email@mail.com", "pass","name","lastNmae","description","123123",userImage);
        User savedUser = userRepository.save(user);
        assertNotNull(savedUser.getId());
        assertNotNull(savedUser.getUserImage().getId());
    }

和applicantController.js代码在这里

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);
 });

但api响应是这样的

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响应是404发现的。?

1 个答案:

答案 0 :(得分:0)

看起来你在设置服务器路由的快速应用程序中缺少很多必要的代码。我认为这就是"在这里输入代码"部分是为了。

Here is a quick tutorial关于设置一个非常基本的快递服务器。

对于示例中的URL,您需要一个类似的路径:

#include <stdio.h>
#include <stdlib.h>

// Define a structure type, no builtin tuples
typedef struct { 
    int n;
    int d;
} Fraction;


int main(int argc, char **argv) {
    int i;
    int N = 5; 
    Fraction f0 =  {2, 3};  // Directly create & initialize data 
    Fraction *fractions;    // variable length ponter to array of Fractions 


    // Using dynamic allocation of arrays
    fractions =  (Fraction *) malloc(N*sizeof(Fraction));  
    fractions[0] = f0;                   
    fractions[1] = (Fraction) {5, 7};    // This needs to be explicitly casted to Fraction type
    fractions[2] = (Fraction) {11, 13}; 
    fractions[3] = (Fraction) {17, 19}; 
    fractions[4] = (Fraction) {23, 29}; 


    printf ("Fraction0 -> numerator: %d, denominator: %d\n", f0.n, f0.d); 

    for (i=1; i < N; i++) {
        printf ("Fractions[%d] -> numerator: %d, denominator: %d\n", i, fractions[i].n, fractions[i].d); 
    }
}

此外,无关,但要更多RESTFul,您可能需要稍微更改URL:router.get('/api/applicant/:id/getFullDetails', function(req, res) { // implementation here });