Fahad Subzwari
Fahad Subzwari

Reputation: 2325

Nodejs api 404 not found error while accessing url paremter

i am making a nodejs api in which i am fetching applicant all record from db. my api url is like this

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

and applicantController.js code is here

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

  }

But the api response is like this

Cannot GET /api/applicant/23/getFullDetails 404

Can any one tell me what's wrong here? why api response is 404 found.?

Upvotes: 2

Views: 8860

Answers (1)

Mikelax
Mikelax

Reputation: 572

It looks like you are missing a lot of the necessary code in the express application that sets up the routes for the server. I think that is what the "enter code here" section is for.

Here is a quick tutorial on setting up a very basic express server.

For the URL in your example, you will need a route something like:

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

Also, unrelated but to be more RESTFul, you may want to change the URL slightly to something like: /api/applicants/:id/fullDetails

Upvotes: 1

Related Questions