express-api-versioning icon indicating copy to clipboard operation
express-api-versioning copied to clipboard

It's showing TypeError: expressApiVersioning is not a function

Open 206577873 opened this issue 6 years ago • 6 comments

app.use(expressApiVersioning({ apiPath: path.join(__dirname, './api'), test: //api/(v[0-9]+).*/, entryPoint: 'index.js', instance: null }, (error, req, res, next) => { console.log(error); next(); })); ^

TypeError: expressApiVersioning is not a function

206577873 avatar Sep 27 '18 07:09 206577873

I see two disturbing things in the snippet. First, test should be....

  • test: /\/api/(v[0-9]+).*/,

escape the forward slash. Secondly, instance should be your app, so the instance line should be...

  • instance: app

Try this and if you still get errors then you will have to show me your directory structure

victor-nweze-deel avatar Sep 27 '18 16:09 victor-nweze-deel

If this is hosted on a remote repo.... then you could share the link

victor-nweze-deel avatar Sep 27 '18 16:09 victor-nweze-deel

@ProfJigsaw I don't think you're exporting properly.

I got it to work:

// Instead of this:
const expressApiVersioning = require('express-api-versioning');
// do this
const expressApiVersioning = require('express-api-versioning').default;

arronhunt avatar Oct 24 '18 20:10 arronhunt

Hi, I am using const expressApiVersioning = require('express-api-versioning').default; now & following exactly your mock link.

Directory Structure

app.js
api
    v1
        index.js
        routes
                  index.js
    v2
       index.js
       routes
                 index.js

in root app.js

const expressApiVersioning = require('express-api-versioning').default;
const app = express();

const config = {
  apiPath: path.join(__dirname, './api'),
  test: /\/api\/(v[0-9]+).*/,
  entryPoint: 'index.js',
  instance: app
};
app.use(expressApiVersioning(config, (error, req, res, next) => {
  console.log(error);
  next();
}));

Then in api>v1>index.js

import routes from './routes';
export default (app) => { //console.log(app)
  app.use('/api/v1', routes);
};

In api>v1>routes>index.js

import express from 'express';
const router = express.Router();

router.get('/', (req, res) => res
  .send({
    message: 'You are in /api/v1 end point'
  }));
export default router;

Now I am getting the 404 error with this message The current route is not found. Please try different route for http://localhost:3001/api/v1 although error coming as null

Please help. Thanks.

206577873 avatar Dec 10 '18 08:12 206577873

Hey @206577873 maybe file your issue as a separate issue? It doesn't appear related to this one.

arronhunt avatar Dec 10 '18 23:12 arronhunt

I'm just seeing this now. Sorry I've been busy with other things.

@206577873 have you been able to fix the issue now?

adesege avatar Dec 18 '18 00:12 adesege