gpt4 book ai didi

javascript - Node js : how to export a previously exported function to make it visible

转载 作者:搜寻专家 更新时间:2023-11-01 00:04:20 25 4
gpt4 key购买 nike

假设我有包含以下内容的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com