简体   繁体   English

在 Nodejs 中同时使用 Currying 和 Module.export

[英]Using Currying and Module.export together in Nodejs

/startup/routes.js /startup/routes.js

const express = require("express");
const users = require("../routes/users");

module.exports = function(app) {
  app.use(express.json());
  app.use("/api/users", users);
};

index.js index.js

const express = require("express");
const app = express();
require("./startup/routes")(app);

I am confused with the code above, especially the currying in "index.js".我对上面的代码感到困惑,尤其是“index.js”中的柯里化。 Can someone explains me how it works?有人可以解释一下它是如何工作的吗? Generally i am doing the same job as below.一般来说,我正在做与下面相同的工作。 What is the difference between them?它们之间有什么区别?

index.js index.js

const express = require('express');
const app = express();
app.use(express.json());
app.use('/api/users', require('./routes/api/users'));

routes.js exports a function as its default export. routes.js导出 function 作为其默认导出。 index.js imports and runs the function in one expression: index.js在一个表达式中导入并运行 function:

require("./startup/routes")(app);

That's equivalent to:这相当于:

const routesFunction = require("./startup/routes");
routesFunction(app);

Re your version without a routes.js , it does the same thing the original code does, the only difference is that it's all in one module.重新设置没有routes.js的版本,它和原始代码做的一样,唯一的区别是它都在一个模块中。

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

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