[英]How do you use a JavaScript function from another file
So I have a file:所以我有一个文件:
server.js服务器.js
const express = require("express");
const app = express();
const rateLimit = require("express-rate-limit");
function rateLimiter(request, ms) {}
function startServer(port, dir) {}
And i want to call those two functions in another file我想在另一个文件中调用这两个函数
index.js index.js
const express = require("server.js");
var rate = rateLimiter(100, 60000);
var server = startServer(8080, src);
How can I do that?我怎样才能做到这一点?
write something like:写类似:
module.exports = {rateLimiter, startServer} module.exports = {rateLimiter, startServer}
You need to exports those functions and import them to other files.您需要导出这些函数并将它们导入其他文件。
Here is the export这里是出口
server.js服务器.js
const express = require("express");
const app = express();
const rateLimit = require("express-rate-limit");
function rateLimiter(request, ms) {}
function startServer(port, dir) {}
module.exports = {rateLimiter, startServer}
And here is the import这是导入
index.js index.js
const express = require("server.js");
const {rateLimiter, startServer} = require('./server.js'); // your server.js file
var rate = rateLimiter(100, 60000);
var server = startServer(8080, src);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.