Route declared properly but still getting a Could not get any response error












0















I have making an API using express and node.



Here is my app.js



const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');

// setup dotenv to read environment variables
dotenv.config()

// Load Environment Varibles
const env = require('./utils/env');

// INIT MONGODB CONNECTION
require('./mongoose');

// create a new express application
const app = express();

// setup bodyparser middleware to read request body in requests
// we're only reading JSON inputs
app.use(bodyParser.json());

// Listen to API routes
const apiRoutes = require('./routes')
app.use('/api', apiRoutes);

// Start listening to requests
app.listen(env.PORT, () => {
console.log(`Server started on PORT ${env.PORT}`);
});


And here is the API routes that are being imported



const express = require('express');
const apiController = require('./apiController');
const apiValidator = require('./apiValidator');
const router = express.Router();

router.post('/login', apiValidator.loginUserValidator, apiController.loginUserController);

router.get('/rand', (req, res) => {
res.send('Some randon text');
});

module.exports = router;


Here is the middleware



const {
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserValidator = (req, res, next) => {
const user = req.body;
if (!user.username) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid username"));
}

if (!user.password) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid password"));
}
if (user.authTokens) {
delete user.authTokens;
}
next();
};

module.exports = {
loginUserValidator
};


Here is the controller



const User = require('./../models/user');
const {
successResponse,
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserController = async (req, res) => {
try {
const user = req.body;
// find if the user already exists
const existingUser = await User.findOne({
username: user.username
});

if (existingUser) {
// user exists. generate token and login user
console.log('Existing user login');
const token = existingUser.generateAuthToken();
return res.status(200).json(successResponse(token));
} else {
console.log('New user login');
const savedUser = await new User(user).save();
const token = savedUser.generateAuthToken();
return res.status(200).json(successResponse(token));
}
} catch (e) {
console.log(e);
return res.status(400).json(failureResponse(errorcodes.ERROR_SERVER_ERROR, "Unable to login user"));
}
};

module.exports = {
loginUserController
};


Here the issue is when I try to hit the login route from Postman, I am getting an error which says Could not get any response.



But when I hit the rand route, the output is correct.
So the issue isn't the arrangement of the code.
Why am I not able to use the login route here?










share|improve this question

























  • What does apiValidator.loginUserValidator and apiController.loginUserController do? Can you show the code?

    – Anand Undavia
    Nov 26 '18 at 18:37











  • You have middleware on your login route (apivalidator). Maybe there's a problem in there?

    – Jim B.
    Nov 26 '18 at 18:38











  • No. The middleware eventually calls next() no matter what and the controller sends out a response at the end. I can paste the code for you.

    – Sriram R
    Nov 26 '18 at 18:39











  • @AnandUndavia added the validator and controller code

    – Sriram R
    Nov 27 '18 at 3:13











  • Looking at your code. the syntax seems to be correct. Could not get any response means that some callback is missing or some operation took too much time. Can you check if new User save or user findone is being completed or not?

    – Aritra Chakraborty
    Nov 27 '18 at 17:11
















0















I have making an API using express and node.



Here is my app.js



const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');

// setup dotenv to read environment variables
dotenv.config()

// Load Environment Varibles
const env = require('./utils/env');

// INIT MONGODB CONNECTION
require('./mongoose');

// create a new express application
const app = express();

// setup bodyparser middleware to read request body in requests
// we're only reading JSON inputs
app.use(bodyParser.json());

// Listen to API routes
const apiRoutes = require('./routes')
app.use('/api', apiRoutes);

// Start listening to requests
app.listen(env.PORT, () => {
console.log(`Server started on PORT ${env.PORT}`);
});


And here is the API routes that are being imported



const express = require('express');
const apiController = require('./apiController');
const apiValidator = require('./apiValidator');
const router = express.Router();

router.post('/login', apiValidator.loginUserValidator, apiController.loginUserController);

router.get('/rand', (req, res) => {
res.send('Some randon text');
});

module.exports = router;


Here is the middleware



const {
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserValidator = (req, res, next) => {
const user = req.body;
if (!user.username) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid username"));
}

if (!user.password) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid password"));
}
if (user.authTokens) {
delete user.authTokens;
}
next();
};

module.exports = {
loginUserValidator
};


Here is the controller



const User = require('./../models/user');
const {
successResponse,
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserController = async (req, res) => {
try {
const user = req.body;
// find if the user already exists
const existingUser = await User.findOne({
username: user.username
});

if (existingUser) {
// user exists. generate token and login user
console.log('Existing user login');
const token = existingUser.generateAuthToken();
return res.status(200).json(successResponse(token));
} else {
console.log('New user login');
const savedUser = await new User(user).save();
const token = savedUser.generateAuthToken();
return res.status(200).json(successResponse(token));
}
} catch (e) {
console.log(e);
return res.status(400).json(failureResponse(errorcodes.ERROR_SERVER_ERROR, "Unable to login user"));
}
};

module.exports = {
loginUserController
};


Here the issue is when I try to hit the login route from Postman, I am getting an error which says Could not get any response.



But when I hit the rand route, the output is correct.
So the issue isn't the arrangement of the code.
Why am I not able to use the login route here?










share|improve this question

























  • What does apiValidator.loginUserValidator and apiController.loginUserController do? Can you show the code?

    – Anand Undavia
    Nov 26 '18 at 18:37











  • You have middleware on your login route (apivalidator). Maybe there's a problem in there?

    – Jim B.
    Nov 26 '18 at 18:38











  • No. The middleware eventually calls next() no matter what and the controller sends out a response at the end. I can paste the code for you.

    – Sriram R
    Nov 26 '18 at 18:39











  • @AnandUndavia added the validator and controller code

    – Sriram R
    Nov 27 '18 at 3:13











  • Looking at your code. the syntax seems to be correct. Could not get any response means that some callback is missing or some operation took too much time. Can you check if new User save or user findone is being completed or not?

    – Aritra Chakraborty
    Nov 27 '18 at 17:11














0












0








0








I have making an API using express and node.



Here is my app.js



const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');

// setup dotenv to read environment variables
dotenv.config()

// Load Environment Varibles
const env = require('./utils/env');

// INIT MONGODB CONNECTION
require('./mongoose');

// create a new express application
const app = express();

// setup bodyparser middleware to read request body in requests
// we're only reading JSON inputs
app.use(bodyParser.json());

// Listen to API routes
const apiRoutes = require('./routes')
app.use('/api', apiRoutes);

// Start listening to requests
app.listen(env.PORT, () => {
console.log(`Server started on PORT ${env.PORT}`);
});


And here is the API routes that are being imported



const express = require('express');
const apiController = require('./apiController');
const apiValidator = require('./apiValidator');
const router = express.Router();

router.post('/login', apiValidator.loginUserValidator, apiController.loginUserController);

router.get('/rand', (req, res) => {
res.send('Some randon text');
});

module.exports = router;


Here is the middleware



const {
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserValidator = (req, res, next) => {
const user = req.body;
if (!user.username) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid username"));
}

if (!user.password) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid password"));
}
if (user.authTokens) {
delete user.authTokens;
}
next();
};

module.exports = {
loginUserValidator
};


Here is the controller



const User = require('./../models/user');
const {
successResponse,
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserController = async (req, res) => {
try {
const user = req.body;
// find if the user already exists
const existingUser = await User.findOne({
username: user.username
});

if (existingUser) {
// user exists. generate token and login user
console.log('Existing user login');
const token = existingUser.generateAuthToken();
return res.status(200).json(successResponse(token));
} else {
console.log('New user login');
const savedUser = await new User(user).save();
const token = savedUser.generateAuthToken();
return res.status(200).json(successResponse(token));
}
} catch (e) {
console.log(e);
return res.status(400).json(failureResponse(errorcodes.ERROR_SERVER_ERROR, "Unable to login user"));
}
};

module.exports = {
loginUserController
};


Here the issue is when I try to hit the login route from Postman, I am getting an error which says Could not get any response.



But when I hit the rand route, the output is correct.
So the issue isn't the arrangement of the code.
Why am I not able to use the login route here?










share|improve this question
















I have making an API using express and node.



Here is my app.js



const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');

// setup dotenv to read environment variables
dotenv.config()

// Load Environment Varibles
const env = require('./utils/env');

// INIT MONGODB CONNECTION
require('./mongoose');

// create a new express application
const app = express();

// setup bodyparser middleware to read request body in requests
// we're only reading JSON inputs
app.use(bodyParser.json());

// Listen to API routes
const apiRoutes = require('./routes')
app.use('/api', apiRoutes);

// Start listening to requests
app.listen(env.PORT, () => {
console.log(`Server started on PORT ${env.PORT}`);
});


And here is the API routes that are being imported



const express = require('express');
const apiController = require('./apiController');
const apiValidator = require('./apiValidator');
const router = express.Router();

router.post('/login', apiValidator.loginUserValidator, apiController.loginUserController);

router.get('/rand', (req, res) => {
res.send('Some randon text');
});

module.exports = router;


Here is the middleware



const {
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserValidator = (req, res, next) => {
const user = req.body;
if (!user.username) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid username"));
}

if (!user.password) {
return res.status(400).json(failureResponse(errorcodes.ERROR_INVALID_BODY_PARAMETER, "Invalid password"));
}
if (user.authTokens) {
delete user.authTokens;
}
next();
};

module.exports = {
loginUserValidator
};


Here is the controller



const User = require('./../models/user');
const {
successResponse,
failureResponse
} = require('./../utils/response');
const errorcodes = require('./../utils/errorcodes');

const loginUserController = async (req, res) => {
try {
const user = req.body;
// find if the user already exists
const existingUser = await User.findOne({
username: user.username
});

if (existingUser) {
// user exists. generate token and login user
console.log('Existing user login');
const token = existingUser.generateAuthToken();
return res.status(200).json(successResponse(token));
} else {
console.log('New user login');
const savedUser = await new User(user).save();
const token = savedUser.generateAuthToken();
return res.status(200).json(successResponse(token));
}
} catch (e) {
console.log(e);
return res.status(400).json(failureResponse(errorcodes.ERROR_SERVER_ERROR, "Unable to login user"));
}
};

module.exports = {
loginUserController
};


Here the issue is when I try to hit the login route from Postman, I am getting an error which says Could not get any response.



But when I hit the rand route, the output is correct.
So the issue isn't the arrangement of the code.
Why am I not able to use the login route here?







node.js rest express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 18:41







Sriram R

















asked Nov 26 '18 at 18:34









Sriram RSriram R

605716




605716













  • What does apiValidator.loginUserValidator and apiController.loginUserController do? Can you show the code?

    – Anand Undavia
    Nov 26 '18 at 18:37











  • You have middleware on your login route (apivalidator). Maybe there's a problem in there?

    – Jim B.
    Nov 26 '18 at 18:38











  • No. The middleware eventually calls next() no matter what and the controller sends out a response at the end. I can paste the code for you.

    – Sriram R
    Nov 26 '18 at 18:39











  • @AnandUndavia added the validator and controller code

    – Sriram R
    Nov 27 '18 at 3:13











  • Looking at your code. the syntax seems to be correct. Could not get any response means that some callback is missing or some operation took too much time. Can you check if new User save or user findone is being completed or not?

    – Aritra Chakraborty
    Nov 27 '18 at 17:11



















  • What does apiValidator.loginUserValidator and apiController.loginUserController do? Can you show the code?

    – Anand Undavia
    Nov 26 '18 at 18:37











  • You have middleware on your login route (apivalidator). Maybe there's a problem in there?

    – Jim B.
    Nov 26 '18 at 18:38











  • No. The middleware eventually calls next() no matter what and the controller sends out a response at the end. I can paste the code for you.

    – Sriram R
    Nov 26 '18 at 18:39











  • @AnandUndavia added the validator and controller code

    – Sriram R
    Nov 27 '18 at 3:13











  • Looking at your code. the syntax seems to be correct. Could not get any response means that some callback is missing or some operation took too much time. Can you check if new User save or user findone is being completed or not?

    – Aritra Chakraborty
    Nov 27 '18 at 17:11

















What does apiValidator.loginUserValidator and apiController.loginUserController do? Can you show the code?

– Anand Undavia
Nov 26 '18 at 18:37





What does apiValidator.loginUserValidator and apiController.loginUserController do? Can you show the code?

– Anand Undavia
Nov 26 '18 at 18:37













You have middleware on your login route (apivalidator). Maybe there's a problem in there?

– Jim B.
Nov 26 '18 at 18:38





You have middleware on your login route (apivalidator). Maybe there's a problem in there?

– Jim B.
Nov 26 '18 at 18:38













No. The middleware eventually calls next() no matter what and the controller sends out a response at the end. I can paste the code for you.

– Sriram R
Nov 26 '18 at 18:39





No. The middleware eventually calls next() no matter what and the controller sends out a response at the end. I can paste the code for you.

– Sriram R
Nov 26 '18 at 18:39













@AnandUndavia added the validator and controller code

– Sriram R
Nov 27 '18 at 3:13





@AnandUndavia added the validator and controller code

– Sriram R
Nov 27 '18 at 3:13













Looking at your code. the syntax seems to be correct. Could not get any response means that some callback is missing or some operation took too much time. Can you check if new User save or user findone is being completed or not?

– Aritra Chakraborty
Nov 27 '18 at 17:11





Looking at your code. the syntax seems to be correct. Could not get any response means that some callback is missing or some operation took too much time. Can you check if new User save or user findone is being completed or not?

– Aritra Chakraborty
Nov 27 '18 at 17:11












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487091%2froute-declared-properly-but-still-getting-a-could-not-get-any-response-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487091%2froute-declared-properly-but-still-getting-a-could-not-get-any-response-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks