简体   繁体   English

你如何使用另一个文件中的 JavaScript function

[英]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.

相关问题 如何使用JavaScript中另一个文件中的变量和函数? - How do I use variables and function from another file in javascript? 如何从另一个 javascript 文件的 function 中访问 var? - How do you access a var from inside another javascript file's function? 我如何使用函数的JSON结果,我在另一个javascript文件中调用 - How do i use the JSON result from a function, which i call in another javascript file 如何使用另一个Javascript文件中的函数? - How can I use a function from one Javascript file in another? 如何使用 jQuery 从另一个文件调用 JavaScript function - How to use jQuery to call a JavaScript function from another file 如何从JavaScript中的另一个外部函数访问嵌套的内部函数? - How do you access an nested inner function from another outer function in JavaScript? 如何使用Node require在另一个Javascript文件中定义类的功能? - How do you define a function of a class in another Javascript file using Node require? 如何从 js 文件上的一个 function 获取变量并将其用于另一个 js 文件上的另一个 function? - How do I get a variable from one function on a js file and use it for another function on another js file? 如何使用另一个JavaScript文件中定义的功能? - how to use a function defined in another javascript file? 如何在另一个JavaScript文件的功能内使用远程存储的JavaScript文件中的变量? - How could I use the variables from a remotely stored JavaScript file within the function of another JavaScript file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM