Search code examples
firebasevue.jsgoogle-cloud-functionsfirebase-hosting

how to deploy nodejs server (that sends Email) and vuejs app in one server on firebase hosting


This might sound strange, I know... people will think I should have used firebase cloud function to send the Mail, Yes I did, unexpectedly, it just stopped delivery and I couldn't find a solution to it.

I created a server in Nodejs and hook up my frontend to send a POST request to the server. now I want to deploy that server to firebase hosting, so the server will get hosted alongside the app.

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;
          });
      }
    }

so, is there I way I could deploy that localhost to firebase, so the post request could get sent without switching on the server on node


Solution

  • You cannot run a regular Node.js script on Firebase Hosting. The closest you can get is converting that script into Cloud Functions.

    Many Node.js apps can be converted, but opening a local post (as seems to be the case for you) is not a good option on Cloud Functions. I'd typically convert your sendEmail endpoint into a Callable or HTTPS triggered Cloud Function.