Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4324358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:07:18+00:00 2026-05-21T09:07:18+00:00

I’m building my node.js app which has the following structure: server.js controllers/user.js server.js require

  • 0

I’m building my node.js app which has the following structure:

  • server.js
  • controllers/user.js

server.js require the user.js controller with:

require('./controllers/user.js').route(app, mongoose);

the controller/user.js file is like:

function route(app, mongoose){
   function route(app, mongoose){

   // Create new User item
   app.post('/user/create', function(req, res){
     ...
   }

   // Edit user
   app.put('/user/:id/edit', function(req, res){
    ...
   }

   ...
}
module.exports.route = route;

This is working fine.
I know want to had middleware in the Edit user function for instance so it looks like:

...
app.put('/user/:id/edit', loadUser, function(req, res){
...

If I define loadUser function right above this line it’s working fine. When I add all the middleware fonction in a file ‘./lib/middleware.js’ and when I try to load that file in user.js with:

require('../lib/middleware.js').create(); // Create is the exported function

this does not work and I have the error message saying that loadUser is an unknow function.

Any idea ?

** UPDATE **

I have updated the files such that, in server.js (main file) I have:

...
var middleware = require('./lib/middleware.js');
...
require('./controllers/user.js').route(app, mongoose, middleware);
...

In middleware.js, I then have:

function create() {
  function loadUser(req, res, next) {
    // You would fetch your user from the db
    var user = users[req.params.id];
    if (user) {
      req.user = user;
      next();
    } else {
      next(new Error('Failed to load user ' + req.params.id));
    }
  }
return module;
}

In controllers/user.js I have

function route(app, mongoose, middleware){
   ...
   // Modify an user
   app.put('/user/edit', middleware.loadUser, function(req, res){
     ...
   }
   ...
}

When I run the app (node server.js) I then have the following error:

Error: PUT route /user/edit requires a callback

I am not sure to return the correct thing within middleware.js, not really familiar with module stuff yet. I also tried the “module.exports.create = create;” but same thing.

UPDATE WITH ANOTHER TRY

what if I create a module for the function ? In ./lib/middleware.js I would have:

(function(){
  var middleware = {};
  middleware.loadUser = function () {
  console.log("loadUser");
  }
  return middleware;
}());

And in server, I call it:

var middleware = require('./lib/middleware.js');
middleware.loadUser;

It seems to me that should work but it does not…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:07:19+00:00Added an answer on May 21, 2026 at 9:07 am

    “global” scope in a file is actually module scope. Just by creating a function in a different file it does become in scope in your original file.

    What you want to do instead is

    // routes.js
    var middleware = require("../lib/middleware.js").create();
    
    app.put('/user/:id/edit', middelware["loadUser"], function(req, res){
    

    You will find that global variables actually write to module in their scope.

    Then your function loadUser() { ... } should exist in the module property.

    // middleware.js
    function create() { 
        ...
    
        return module;
    }
    

    If you return module from your create function your returning global scope.

    [Edit]

    function create() {
      function loadUser(req, res, next) {
        // You would fetch your user from the db
        var user = users[req.params.id];
        if (user) {
          req.user = user;
          next();
        } else {
          next(new Error('Failed to load user ' + req.params.id));
        }
      }
    return module;
    }
    

    You either need to add module.loadUser = loadUser or define loadUser in module scope. I.e. outside the create function.

    [Further Edit]:

    A standard setup would be something like:

    // middleware.js
    (function() {
    
        function loadUser(...) {
            ...
        }
    
        ...
    
        module.exports.loadUser = loadUser;
    
    })();
    
    //otherfile.js
    var middle = require("middleware");
    middle.loadUser();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.