dimanche 1 avril 2018

Can't get Express router to work

I'm currently getting into NodeJS in my free time and I tried to set up a simple project to play around with Node and GraphQL by splitting things into module as much as possible, which leads me to this folder structure.
The issue I'm running into is that none of the routes defined via Express get triggered. The page just loads forever until it times out. Here's the code for the server's entry point:

// index.js

// index.js
const appConfig = require('./config');
const express = require('express');
var app = express();

// Set up configuration variables according to the current execution environment
const environment = appConfig.environment;
const config = appConfig[environment];
const defaultPort = config.app.port;
const logLevel = config.app.logLevel;

// Set up modules
const logger = require('./src/logger')('server', logLevel);
const db = require('./src/db')(config);
const handler = require('./src/handler')(db);
const routes = require('./src/routes')(handler);

// Set up routes
app.use('/', routes);

// Start listening
const server = app.listen(process.env.PORT || defaultPort, function() {
    const port = server.address().port;
    logger.log('info', 'App now listening to port ' + port);
});  

And the routing module:

// src/routes/index.js
const logger = require('../logger')('routes');
const bodyParser = require('body-parser');
const express = require('express');
const router = express.Router();

module.exports = function(handler) {

    logger.log('info', 'Setting up routes.')

    router.use(bodyParser.json);

    router.get('/', (req, res) => {
        logger.log('info', '/');
        res.send('Home Page');
    });

    router.get('/quotes', (req, res) => {
        logger.log('info', '/quotes');
        res.send('/quotes');
    });

    return router;
}   

The "handler" being passed to routes is meant to hold the functions that will be called by the routes.
I'm obviously missing something but I can't figure out what it is, so if anyone has any idea it'll be greatly appreciated.

Quick note: if I pass "app" to the routing module and call app.use('/', router) right before "return router", it works fine. But that completely defeats the purpose of splitting this into two modules.




Aucun commentaire:

Enregistrer un commentaire