首页 > 解决方案 > 如何在firebase托管的一台服务器上部署nodejs服务器(发送电子邮件)和vuejs应用程序

问题描述

这可能听起来很奇怪,我知道...人们会认为我应该使用 Firebase 云功能来发送邮件,是的,我确实这样做了,但意外的是,它只是停止了发送,我找不到解决方案。

我在 Nodejs 中创建了一个服务器,并连接了我的前端以向服务器发送一个 POST 请求。现在我想将该服务器部署到 Firebase 托管,因此该服务器将与应用程序一起托管。

sendMessage() {
      if (this.$refs.formEmail.validate()) {
        this.loading = true;
        this.disabled = true;
        const data = {
          name: this.formData.name,
          email: this.formData.email,
          message: this.formData.message
        };
        axios
          .post("http://localhost:4000/sendEmail", data)
          // eslint-disable-next-line no-unused-vars
          .then(res => {
            this.disabled = false;
            this.loading = false;
            this.snackbar = true;
            this.successMessage = "Mail sent! We'll respond ASAP.";
            this.$refs.formEmail.reset();
          })
          .catch(err => {
            this.disabled = false;
            this.loading = false;
            this.snackbarError = true;
            this.errorMessage = err.message;
          });
      }
    }

那么,有没有我可以将该本地主机部署到firebase的方法,因此可以在不打开节点上的服务器的情况下发送发布请求

标签: firebasevue.jsgoogle-cloud-functionsfirebase-hosting

解决方案


您无法在 Firebase 托管上运行常规 Node.js 脚本。您可以获得的最接近的方法是将该脚本转换为 Cloud Functions。

许多 Node.js 应用程序都可以转换,但在 Cloud Functions 上打开本地帖子(您似乎就是这种情况)不是一个好的选择。我通常会将您的sendEmail端点转换为可调用HTTPS 触发的云函数。


推荐阅读