tommyd456
tommyd456

Reputation: 10703

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:

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

and my routes file:

//server/routes/index.js
var express = require('express');
var router = express.Router();

module.exports = function() {
  router.get('/test', function(req, res, next){
    console.log("HIT");
    res.status(200).send("OK");
  });

  return router;
}

Everytime I navigate to /server/test 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?

Upvotes: 2

Views: 1863

Answers (1)

Nick Tomlin
Nick Tomlin

Reputation: 29261

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.

Upvotes: 4

Related Questions