[英]Javascript function using express.js routing
I'm new to javascript, and recently learning the MEAN stack. 我是javascript的新手,最近学习了MEAN堆栈。 I get the gist of how this works, but I don't understand how the module.exports function is using the require function:
我了解其工作原理,但是我不了解module.exports函数如何使用require函数:
var express = require('express');
module.exports = function() {
var app = express();
require('../app/routes/index.server.routes.js')(app);
return app;
};
According to the book, it requires the routing file in one argument, and passes it to the express instance 'app'. 根据这本书,它需要在一个参数中包含路由文件,并将其传递给快速实例“ app”。 Using this logic, you can pass a function to another:
使用此逻辑,可以将一个函数传递给另一个函数:
var a = function_a();
function_b(...)(a);
Can someone explain this to me? 谁可以给我解释一下这个?
in your example the file required in the function exported returns a function and that function is invoced immediately passing as its first argument the express app you have created 在您的示例中,导出的函数中所需的文件返回一个函数,并且立即调用该函数,将您创建的Express应用作为其第一个参数传递
this could be written like so 可以这样写
let createroutes = require('../app/routes/index.server.routes.js');
createroutes(app);
most probably the index.server.routes.js
file has a structure like this index.server.routes.js
文件很可能具有这样的结构
let express = require("express");
module.exports = (app) => {
let Controllers = require("./controllers");
let router = express.Router();
router.get("/user", Controllers.user)
app.use("/", router);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.