0

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(),
};

1 Answer 1

2

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Yes those were already set. I was using Object.keys instead of Object.values. Thanks man <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.