0

I have built a simple koa framework application.After adding routes am trying to hit the /health GET route. It throws the below error:

TypeError: next is not a function
      at cookieParser (c:\Userxxs\x\gimt\dp-my-app\node_modules\cookie-parser\index.js:46:14)
      at dispatch (c:\Users\B748806a\gimt\dp-my-app\node_modules\koa-compose\index.js:42:32)
      at bodyParser (c:\Users\B748806a\gimt\dp-my-app\node_modules\koa-bodyparser\index.js:95:11)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

Below are the files and their order of execution: server.js

const app = require("./app.js");
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Listening on port: ${PORT}`));

app.js

"use strict";
const koa = require('koa');
const koaRouter = require('koa-router');
const app = new koa();
const router = new koaRouter();
const middleware = require("./src/main/middlewares/middlewares");
const routes = require("./src/main/middlewares/route-path");

const init = async () => {
  try {
    /**
     * Step 2: load endpoint routes for the application
     */
    routes(router)
   } catch (err) {
    logger.error({
      err
    });
  }
};
/**
 * Step 1: load middleware setup - cors,helmet from KP Spring cloud service
 */
middleware(app);
init();

module.exports = app

middleware.js

const koa = require("koa");
const koaRouter = require('koa-router');
const router = new koaRouter();
const cors = require("koa-cors");
const compression = require("koa-compress");
const helmet = require("koa-helmet");
const cookieParser = require("cookie-parser");
const bodyParser = require('koa-bodyparser')
const ActuatorRouter = require('pm-js-actuator').koa //internal library
const ACTUATOR_OPTIONS = {
  health: {
      enabled: true
  },
  livenessprobe: {
      enabled: true
  },
  env: {
      enabled: false
  },
  info: {
      enabled: true,
      secure: false
  }
}
function middleware(app) {
  // Use the CORS for the time being.
  app.use(cors())

  // Let's don the security helmet
  app.use(helmet())
  app.use(helmet.frameguard())
  app.use(helmet.ieNoOpen())
  app.use(helmet.frameguard({
    action: 'sameorigin'
  }))
  app.use(helmet.noSniff())
  app.use(helmet.referrerPolicy({
    policy: 'same-origin'
  }))
  app.use(helmet.xssFilter())
  //app.disable('x-powered-by')
  app.use(ActuatorRouter.getRouter(ACTUATOR_OPTIONS).routes())

  app.use(bodyParser())

  app.use(cookieParser());
  // Set up compression
  app.use(compression());
}

module.exports = middleware;

route-path.js

 const RootHeathController = require("../controller/root-health-controller")
 const routes = (router) => {
  router.get("/health", RootHeathController.handler)

};
module.exports = routes

root-health-controller.js

const handler = async (ctx, next) => { 
  ctx.body="Hi";
}
module.exports = {
  handler
};

The application is started successfully on port 3000. But when i hit, /health from postman, it throws the mentioned error. Any solution?

KGT
  • 29
  • 3

1 Answers1

0

The problem here is, that cookie-parser seems to be an express - thing (see also repo url: https://github.com/expressjs/cookie-parser). So to test this I created a minimal version of your code:

const koa = require('koa');
const koaRouter = require('koa-router');
const bodyParser = require('koa-bodyparser');
const cookieParser = require("cookie-parser");

const app = new koa();

app.use(bodyParser());
app.use(cookieParser());   // <-- comment this line

const router = new koaRouter();

router.get("/health", async (ctx, next) => {
    ctx.body = 'hi';
});

app.use(router.routes());

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Listening on port: ${PORT}`));

Calling the localist:3000/health endpoint throws the same error. But if you comment the app.use(cookie-parser()) line all works fine.

The question is, why you would need this library? You should be able to set and get cookies in koa with ctx.cookies.get and ctx.cookies.set

Sebastian Hildebrandt
  • 2,661
  • 1
  • 14
  • 20