简体   繁体   English

ExpressJS-路由功能

[英]ExpressJS - Routing Function

I created a separate JS file for specific server functions. 我为特定的服务器功能创建了一个单独的JS文件。 Sample code as follow: 示例代码如下:

my-data.js my-data.js

var exports = module.exports = {};
exports.getData = function (request, response) {
    var myData = [
        {
            "value": 1
        },
        {
            "value": 2
        },
    ];
    response.json(myData);
};

In my app.js , I'm trying to call that specific function when the request has been made. 在我的app.js中 ,我试图在发出请求时调用该特定函数。

Working sample of app.js app.js的工作示例

var express = require("express");
var app = express();
var port = process.env.PORT || 3000;
var myData = require("./lib/my-data.js");

app.engine(".html", require("ejs").__express);
app.set("views", __dirname + "/views");
app.set("view engine", "html");
app.use(express.static(__dirname));

// Line below is what I'm trying to achieve.
//app.get("/get-data", myData.getData(request, response));

// Working line
app.get("/get-data", function(request, response) {
    myData.getData(request, response);
});

app.get("*", function (request, response) {
    response.render("index");
});

app.listen(port, function () {
    console.log("Listening on port " + port);
});

It bothers me that the line app.get("/get-data", myData.getData(request, response)); app.get("/get-data", myData.getData(request, response)); doesn't work while 一会儿不起作用

app.get("/get-data", function(request, response) {
        myData.getData(request, response);
});

does. 做。

What is the difference between the two approach? 两种方法有什么区别?

I prefer using the first one since it's clean and precise but I can't seem to make it work. 我喜欢使用第一个,因为它干净且精确,但我似乎无法使其正常工作。

app.get("/get-data", myData.getData); fixes this issue. 解决此问题。

app.get allows you to supply a callback function , which is the one that you supply as the second parameter. app.get允许您提供一个回调函数 ,这是您提供的第二个参数。

I'm going to simplify this problem and call that function get(string, func) . 我将简化此问题,并调用该函数get(string, func) Inside this function, typically it's going to call the function that you supplied: 在此函数内部,通常将调用您提供的函数:

function get(string, func) {
  var request = "foo";
  var response = "bar";
  // ...
  func(request, response);
}

So you have to pass the name of the function for it to correctly call your function, therefore app.get("/get-data", myData.getData); 因此,您必须传递函数名称才能正确调用您的函数,因此app.get("/get-data", myData.getData); works. 作品。

However, If you supply the second parameter as this get("/get-data", myData.getData(request, response)); 但是,如果提供第二个参数作为此get("/get-data", myData.getData(request, response)); , as in your first case, you are no longer supplying the func parameter with a function but just a value returned from your myData.getData(...) function. 与第一种情况一样,您不再为func参数提供函数,而只是为myData.getData(...)函数返回的值。

For example, if your function is this: 例如,如果您的函数是这样的:

function getData(req, res) {
  return 1;
}

The get function will end up doing something like: get函数最终将执行以下操作:

get("foo", 1) {
  // ...
  1(request, response);
}

In the second case: 在第二种情况下:

app.get("/get-data", function(request, response) {
  myData.getData(request, response);
});

This now passes an anonymous function as the callback and inside it you are just calling your own function and therefore will work as expected. 现在,它将传递一个匿名函数作为回调,并且在其中您只是在调用自己的函数,因此将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM