tommyd456 tommyd456 - 1 year ago 72
Node.js Question

Express route not returning

Probably a silly mistake but I cannot see why I'm not getting a response from a GET request.

This is part of my server file located in the root:

var serverRoutes = require('./server/routes');
app.use('/server', serverRoutes);

and my routes file:

var express = require('express');
var router = express.Router();

module.exports = function() {
router.get('/test', function(req, res, next){

return router;

Everytime I navigate to
in my browser it just stalls. Nothing is logged in the terminal and no "OK" response is received in the browser - what am I missing?

Answer Source

Your serverRoutes module exports a function that returns a router. You need to invoke it in order to pass the Router instance it returns to app.use:

app.use('/server', serverRoutes());

Otherwise, express is going to treat your exported function as a middleware, which will cause the app to hang since it does nothing with the response passed to it.