javascriptnode.jsdockerexpressexpress-router

How to group express routes like express-group-routes?


I tried with express-group-routes and works very well on my localhost but does not detect any routes when is started on a docker container

I have simple code that returns JSON as follows:

{
    "message": "Service Running"
}

and other routes.

The small Expressjs service is working great with node src/server.js on Ubuntu and every routes are found.

The problem comes when I containerize the code.

The DockerFile looks like this

FROM node:12.18.2-alpine3.12

ENV PORT=3000

EXPOSE ${PORT}

COPY . /app

WORKDIR /app

RUN npm install

CMD ["node", "src/server.js"]

The server.js looks like this

const express = require("express");
const bodyParser = require("body-parser");
const app = express();

// parse requests of content-type: application/json
app.use(bodyParser.json());

// parse requests of content-type: application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

var { router } = require('./routes/index');

const port = process.env.PORT || 3000;
app.use(router);

app.listen(port,() => console.log("Server started :::", new Date().getTime()));

and the route file looks like this

const util = require('../util/util');
const auth = require("../controllers/authController.js");
const app = require('express');
const { check, validationResult, query } = require("express-validator");
const router = app.Router();
require('express-group-routes');
module.exports = (router) => {
    router.group('/api', (router) => {

        router.get("/", (req,res) => {
            return res.status(200).send({ message: "Service Running" });
        });


        router.get("/users", (req, res) => {
            const errors = validationResult(req);
            if (!errors.isEmpty()) {
                return util.ResponseUtil.generateResponseJSON(res, 503, 'Error in validation', { errors: errors.mapped() });
            } else {
                auth.userInfo(req, res);
            }
        });

    });
}
    

  1. when running the docker container docker run -it -p 3000:3000 service:latest

  2. The app is running and I get Server started ::: 1594299481981

  3. I'm trying to GET http://localhost:3000/api/

  4. It returns me

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>Cannot GET /api/</pre>
</body>

</html>

What I tried


Solution

  • express-group-routes is not actively maintained (4 years ago)

    Also its npm site https://www.npmjs.com/package/express-group-routes does not have a github repository!

    Try to use this snippet:

    https://github.com/jrichardsz/nodejs-express-snippets/blob/master/hello-world.js

    In which I show you a basic example of routes.

    If you want to group your routes you could use something like this:

    server.js

    const app = require('express');
    const AdminRoute = require ...
    const UserRoute = require ...
    ....
    AdminRoute.configure(app)
    UserRoute.configure(app)
    

    Or this

      fs.readdirSync(`/some/folder/routes/`).forEach(function(file){
        var commandRequire = require(`/some/folder/routes/`+file);
        var command = new commandRequire();
      });
    

    In which I read js files from a folder and I instantiate them dynamically, so your server.js will be more reduced:

    const app = require('express');
    const ScanRoutes = require ...
    ...
    ScanRoutes.scan("routes/folder", app)