Thanveer Shah
Thanveer Shah

Reputation: 3333

App.use is not a function error in Express js

I am trying to create a seperate file that will hold all the modules that I'm using in the project. But it is giving me app.use is not a function error.

I'v been spending alot of time on this. So, I just wanted to know if this is even possible.

Code that I have tried so far.

index.js

const requireModules = require("./require");

for (let keys in requireModules) {
   app.use(keys);
}

Require.js

const cors = require("cors");
const morgan = require("morgan");
const helmet = require("helmet");

module.exports = {
   cors: cors(),
   morgan: morgan("common"),
   helmet: helmet(),
};

Upvotes: 0

Views: 3299

Answers (1)

Iyanu Adelekan
Iyanu Adelekan

Reputation: 406

You must create an app instance before invoking app.use():

const requireModules = require("./require");
const express = require('express');
const app = express(); // app instance.

Object.values(requireModules).forEach(lib => app.use(lib));

Upvotes: 2

Related Questions