Express.js - 路由文件中的导出/访问功能

时间:2016-06-20 10:57:01

标签: node.js express

我正在尝试在路径文件中导出函数。这是我的代码

app.js文件

var express = require('express');
var router = express.Router();
router.post("/test", function (req, res, next) {
    res.send("success");
});
//Here i want to export function to users js file
var funDef=function(){
console.log("export a function")
}    

moudle.exports=router;

others.js file

var express = require('express');
var router = express.Router();
var readFun= require("./others.js");

router.post("/testUsers", function (req, res, next) {   
res.send("suceess");
});   
//getting undefined
readFun.funDef();

module.exports=router;

如何在users.js文件中导出或调用funDef。我尝试了以下方式,但它无法正常工作

users.js文件

ParseObject object = new ParseObject("Appetizers"); // class name

    ParseFile postImage = object.getParseFile("imageFiles"); // column name

    ParseQuery<ParseObject> getimage = new ParseQuery<ParseObject>("Appetizers"); // class
    getimage.findInBackground(new FindCallback<ParseObject>() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            // TODO Auto-generated method stub
            if (e == null) {
                // success
                for (ParseObject parseObject : objects) {
                    ParseFile fileObject = (ParseFile) parseObject.get("imageFiles");

                    Log.d("test", "get your image ... " + fileObject.getUrl());
                    Picasso.with(getBaseContext()).load(fileObject.getUrl()).placeholder(R.drawable.ic_launcher)
                            .into(mImage);
                }
            } else {
                // fail
                Log.d("test", "error Message... " + e.getMessage());
            }
        }
    });

任何人都可以指导我吗?

1 个答案:

答案 0 :(得分:3)

尝试以下方法:

function router() {}

        router.prototype.funDef=function(){
          console.log("export a function")
        }    
    moudle.exports.router=router;

另一个档案:

 var router = require('router').router;
 var Router = new router();

Router.funDef();

编辑:

// module.js
'use strict';
var temp = "this is a test";

module.exports.func = function() {
   return temp;
}

// app.js
'use strict';
 var module = require('./module');
 console.log(module.func());