Nodejs api 404 not found error while accessing url paremter

194
December 03, 2017, at 6:38 PM

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.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.?

Answer 1

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

READ ALSO
Using Passport to allow types of “user” to authenticate

Using Passport to allow types of “user” to authenticate

Well i have two different type of user which is "Company" and "User" from "Company" and "User" collections respectively? how should i use passportserializeUser and passport

159
Is this way of doing code in node js?

Is this way of doing code in node js?

Well, i am new to node jsI want to know about my coding structure

164
Express/Sequelize — PUT Route to Update Form results in 500 Server Error

Express/Sequelize — PUT Route to Update Form results in 500 Server Error

I'm working on a put route in express to edit details for a bookThere are three required fields --title, author, genre

181