Javascript Node js:如何导出以前导出的函数以使其可见
假设我有post.js,并附上以下内容Javascript Node js:如何导出以前导出的函数以使其可见,javascript,node.js,firebase,google-cloud-functions,systemjs,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. }); 然后我只想将这个函数包含到主文件中,就像它一样,这样当运行index.js(需要post.js)时,post函数就已经导出了 在fire
var functions = require('firebase-functions');
const express = require('express');
exports.post = functions.https.onRequest((req, res) => {
//stuff.
});
然后我只想将这个函数包含到主文件中,就像它一样,这样当运行index.js(需要post.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
,应该是…us-central1/post
另外,是否可以让所需模块从所需文件中引用其变量,这样我就不必在post.js中执行require,对于index.js中已经存在的变量,比如文件系统中的“fs”之类的操作。
谢谢。看来您是在将帖子作为属性导出。您需要将post.js
更改为:
const functions = require('firebase-functions');
const express = require('express');
module.exports = functions.https.onRequest((req, res) => {
//stuff.
});
关于你的问题。这通常是一种不好的做法。每个模块都需要有自己的作用域。因此,您应该使用require
来要求每个文件中所需的每个依赖项。如果您仍然想这样做,可以使用全局变量 好的,谢谢链接,这是正确的。