Returning an Object from middleware function in Node.JS
NickName:Andre Coetzee Ask DateTime:2016-03-12T16:01:52

Returning an Object from middleware function in Node.JS

I am new to Node.JS coming from a Java Background I am using express to build this Rest API . What I am trying to do is build the concept of a manager. I am looking for a elegant way of returning a user object in the following:

users route: user.js

router.get('/find/:email', function(req, res, next){
  userWare.findUserByEmail(req, res, next)
});

middleware/manager: usermiddleware.js

module.exports = {
    findUserByEmail: function(req, res, next) {
        models.core_user.find({
            where:{
                email: req.params.email
            }
        }).then(function(user){
            res.json(user)
        }, function(err){
            res.status(404).json(err);
        });
    },
}

So In this above function I would like to return the user object to the route instead of the json. so that I can create the json from the object in the route. The whole point of this manager class will be to fectch and return objects.

Copyright Notice:Content Author:「Andre Coetzee」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35955217/returning-an-object-from-middleware-function-in-node-js

More about “Returning an Object from middleware function in Node.JS” related questions

Returning an Object from middleware function in Node.JS

I am new to Node.JS coming from a Java Background I am using express to build this Rest API . What I am trying to do is build the concept of a manager. I am looking for a elegant way of returning a...

Show Detail

returning value from a function in middleware in laravel

i've a function in my admin model which has connected to role model by many to many relationship. i created a middleware to check the roles of admins and redirect them to their dashboard, but i'm ...

Show Detail

Writing to Request object in middleware

Is Request object thread-safe in node.js? I'd like to overwrite parameter name having value "Sam" in Request object during middleware execution in router: request from first user App.use(...

Show Detail

Middleware between node.js and Browser?

Is it good idea to implement Middleware service/app between node.js app and browser? For example: Browser <----- [Middleware] <---- [Node JS] Node.js send json meta data to Middleware...

Show Detail

Node.js middleware or not

I'm new to Node.js and I'm migrating a simple site of mine to Node.js mostly as a learning experience. In all my sites, I like to keep the most relevant information on the site in a "sitemeta" ob...

Show Detail

Router.use requires middleware function but got object : node.js / express

I am getting an error when calling my router from my routes directory whenever i try to initialize a route with app.use(). The error i am getting says Router.use() requires a middleware function bu...

Show Detail

Middleware design pattern in Node.js: Connect

This question extends that of What is Node.js' Connect, Express and "middleware"? I'm going the route of learning Javascript -> Node.js -> Connect -> Express -> ... in order to

Show Detail

Redux middleware returning callbacks not state object

I'm new to Redux and I have some confusing questions: 1. The official docs says when using middleware, each the middleware function can return other than new state object(next(action)), such as fu...

Show Detail

Middleware in Node.js with OO JavaScript

Currently I am trying to use the middleware "passport" in an object oriented Node.js server. After restarting the server everything works fine. I can access the route where no authentication is req...

Show Detail

node.js request returning object

it might be a stupid question already but my purpose of this is to return an object called user where I can then use properly. http node.js documentation is full of console.log but nothing mentioned

Show Detail