假设我有包含以下内容的 post.js。
var functions = require('firebase-functions');
const express = require('express');
exports.post = functions.https.onRequest((req, res) => {
//stuff.
});
然后我只想将这个函数包含到主文件中,就这样,当运行需要 post.js 的 index.js 时,post 函数
已经导出。
在 firebase 函数的情况下会运行 https 函数,但现在它不会运行,除非我在需要的文件中再次明确执行 exposts.post。
我试过了。
index.js
// here
exports.post = require("./post");
//Another functions ...
exports.user = functions.https.onRequest((req, res) => {
//stuff
});
但正因为如此,exports.post = require("./post");
,我得到了 http://localhost:5000/project-id/us-central1/post-post
,应该只是 ...us-central1/post
。
另外,是否可以让所需的模块从需要的文件中引用它的变量,这样我就不必在 post.js 中为索引中已经存在的变量做 require .js,文件系统中的“fs”之类的东西。
谢谢。
最佳答案
看来您正在将帖子导出为属性。您需要将 post.js
更改为:
const functions = require('firebase-functions');
const express = require('express');
module.exports = functions.https.onRequest((req, res) => {
//stuff.
});
关于你的问题。这通常是一种不好的做法。每个模块都需要有自己的范围。因此,您应该使用 require
在每个文件中要求您需要的每个依赖项。如果您仍想这样做,可以使用全局变量。 More info
关于javascript - Node js : how to export a previously exported function to make it visible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473515/