Search code examples
expressmoduleroutesrequire

Exporting and importing Express modules


I have 3 files. 1. server.js --> has all the packages and running all the code from this file 2. friends.js -->is a module that carries an array with all the pushed data from client side 3. apiRoutes.js --> is a routing module, that has the task to perform the route /api/friends to display a json object from friends.js

When I import friends.js module and apiRoutes.js module into the server.js, it is not recognizing friends array from friends.js **

How do we get access to data from friends.js when routed as /api/friends when we run server.js

Server.js:

// server.js
//Incorporate dependencies
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');

// console.log(htmlRoutes);
//call express
var app = express();
//requiring htmlRoute.js
require('../app/routing/htmlRoutes.js')(app, path);
//requiring apiRoutes.js
require('../app/routing/apiRoutes.js')(app);

// Declare a port
var PORT = 3000;

//data parsing
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

//requiring the friends.js
require('../app/data/friends.js')


//Listen to port
app.listen(PORT, ()=>{
    console.log('listening to PORT: '+ PORT);
});

friends.js

// friends.js
// array of objects
module.exports = function(app){
var friends =[
    {   
        routeName: "ahmed",
        name:"Ahmed",
        photourl:"https://media.licdn.com/mpr/mpr/shrinknp_400_400/p/6/005/064/1bd/3435aa3.jpg",
        questions:[
                    5,
                    1,
                    4,
                    4,
                    5,
                    1,
                    2,
                    5,
                    4,
                    1
        ]
    }   
]

//retreiving stored objects with person's data
app.post("/survey", function(req, res){
    var incomingPerson = req.body;
    incomingPerson.routeName = incomingPerson.name.replace(/\s+/g, "").toLowerCase();
    console.log(incomingPerson);
    // friends.push(person);
})


}

apiRoutes.js

// apiRoutes.js
//PARAMETERIZATION
module.exports = function(app){
//A GET route with the url `/api/friends`. This will be used to display a JSON of all possible friends.
app.get("/api/friends/:whoDoIWantToSee?", function(req, res){
    var chosen = req.params.whoDoIWantToSee;
    res.json(friends);
    // if(chosen){
    //     res.json(friends.chosen);
    // }

    // console.log(chosen);
});
//A POST routes `/api/friends`. This will be used to handle incoming survey results. This route will also be used to handle the compatibility logic. 
}

Solution

  • You can export the friends array from your friends.js module like this:

    // array of objects
    const friends = [
        {   
            routeName: "ahmed",
            name:"Ahmed",
            photourl:"https://media.licdn.com/mpr/mpr/shrinknp_400_400/p/6/005/064/1bd/3435aa3.jpg",
            questions:[
                        5,
                        1,
                        4,
                        4,
                        5,
                        1,
                        2,
                        5,
                        4,
                        1
            ]
        }   
    ];
    
    module.exports = function(app){
    
        //retreiving stored objects with person's data
        app.post("/survey", function(req, res){
            var incomingPerson = req.body;
            incomingPerson.routeName = incomingPerson.name.replace(/\s+/g, "").toLowerCase();
            console.log(incomingPerson);
            // friends.push(person);
        });
        return friends;
    
    };
    
    module.exports.friends = friends;
    

    Then, anywhere you want access to the singleton friends array, you can do this:

    const friends = require('./friends.js').friends;