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

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

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

相关文章:

客户端的 JavaScript require()

javascript - 如何对嵌套文档进行火存储查询。 Firebase Nodejs

javascript - Ember.js:要编辑什么文件才能将正斜杠附加到每个路由

javascript - NodeJS,gulp,错误:监听 EADDRINUSE:::3000

node.js - express-validator/node-validator : how to validate that property exists, 并且应该存在

node.js - 生成唯一 ID,如 Firebase Firestore

java - 如何在 Firebase 中最好地存储其他数据库对象的 ID(例如 : Event with Users)

javascript - 在 JSP 页面上加载用户帖子

javascript - 如何根据选择值追加表行?

javascript - 仅在需要时加载 javascript 函数