代码之家  ›  专栏  ›  技术社区  ›  Veena Uppalapati

导出和导入Express模块

  •  0
  • Veena Uppalapati  · 技术社区  · 7 年前

    我有3个文件。 1、服务器。js-->包含所有包并运行此文件中的所有代码 2、朋友。js-->是一个模块,它承载一个数组,其中包含来自客户端的所有推送数据 3、APIRouts。js-->是一个路由模块,它的任务是执行路由/api/朋友以显示来自朋友的json对象。js公司

    当我导入朋友时。js模块和apiRoutes。将js模块导入服务器。js,它无法从好友中识别好友数组。js公司 **

    我们如何从朋友那里获取数据。js当路由为时 /当我们运行服务器时,api/朋友。js公司

    服务器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);
    });
    

    朋友。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. 
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   jfriend00    7 年前

    您可以导出 friends 来自朋友的数组。js模块如下:

    // 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;
    

    然后,任何你想访问singleton的地方 朋友 数组,可以执行以下操作:

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