Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create my own module of functions in a node.js app
    primarykey
    data
    text
    <p>I'm building my node.js app which has the following structure:</p> <ul> <li>server.js</li> <li>controllers/user.js</li> </ul> <p>server.js require the user.js controller with:</p> <pre><code>require('./controllers/user.js').route(app, mongoose); </code></pre> <p>the controller/user.js file is like:</p> <pre><code>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; </code></pre> <p>This is working fine. I know want to had middleware in the Edit user function for instance so it looks like:</p> <pre><code>... app.put('/user/:id/edit', loadUser, function(req, res){ ... </code></pre> <p>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:</p> <pre><code>require('../lib/middleware.js').create(); // Create is the exported function </code></pre> <p>this does not work and I have the error message saying that loadUser is an unknow function.</p> <p>Any idea ?</p> <p>** UPDATE **</p> <p>I have updated the files such that, in server.js (main file) I have:</p> <pre><code>... var middleware = require('./lib/middleware.js'); ... require('./controllers/user.js').route(app, mongoose, middleware); ... </code></pre> <p>In middleware.js, I then have:</p> <pre><code>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; } </code></pre> <p>In controllers/user.js I have</p> <pre><code>function route(app, mongoose, middleware){ ... // Modify an user app.put('/user/edit', middleware.loadUser, function(req, res){ ... } ... } </code></pre> <p>When I run the app (node server.js) I then have the following error:</p> <pre><code>Error: PUT route /user/edit requires a callback </code></pre> <p>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.</p> <p><strong>UPDATE WITH ANOTHER TRY</strong></p> <p>what if I create a module for the function ? In ./lib/middleware.js I would have:</p> <pre><code>(function(){ var middleware = {}; middleware.loadUser = function () { console.log("loadUser"); } return middleware; }()); </code></pre> <p>And in server, I call it:</p> <pre><code>var middleware = require('./lib/middleware.js'); middleware.loadUser; </code></pre> <p>It seems to me that should work but it does not...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload