【问题标题】:Node js : how to export a previously exported function to make it visibleNode js:如何导出以前导出的函数以使其可见
【发布时间】:2018-09-03 12:55:34
【问题描述】:

假设我有 post.js 和以下内容。

var functions = require('firebase-functions');
const express = require('express');
exports.post = functions.https.onRequest((req, res) => {
     //stuff.
});

然后我只想将这个函数包含到主文件中,因为它是这样,以便在运行需要 post.js 的 index.js 时,已经导出了 post function

在 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”。

谢谢。

【问题讨论】:

标签: javascript node.js firebase google-cloud-functions systemjs


【解决方案1】:

看来您正在将帖子作为属性导出。您需要将post.js 更改为:

const functions = require('firebase-functions');
const express = require('express');
module.exports = functions.https.onRequest((req, res) => {
    //stuff.
});

关于你的问题。这通常是一种不好的做法。每个模块都需要有自己的范围。因此,您应该使用require 来获取每个文件中所需的每个依赖项。如果你仍然想这样做,你可以使用全局变量。 More info

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多