'Export a function from backend

I have a node.js backend named main.js:

const express = require('express')
var cors = require('cors')
const app = express()

app.use(cors());
app.use('/app', express.static(__dirname + '/ui/dist'));

let count = "1";

app.get('/count', (req, res) => {
  res.json({ "changed": count });
})

const background = function () {
  setTimeout(background, 5000);
  count++;
}

function saveFile () { console.log('entered here') }
// Same behavior when doing:
// const saveFile = function () { console.log('saved') }

background();

app.listen(3000, () => {
  console.log(`MESS (Mongo Event Sourcing) listening at http://localhost:3000`);
});

exports.saveFile = saveFile
// Same behavior when using 
// module.exports = { saveFile }

I wanted to export a function for use in my frontend (Vue).

import axios from 'axios'
import { saveFile } from '../main.js'

//...
mounted: async function () {
    await this.loadCount();
  },
  methods: {
    loadCount: async function () {

      let result = await axios.get(`${process.env.VUE_APP_APIURL}count`);
      this.count = result.data.changed;
      setTimeout(this.loadCount, 3000);

      saveFile()
    }
  }
}

Now my problem is, I am getting an error stating "export 'saveFile' was not found in '../main.js'

And on my browser's dev console, I see another error:

Uncaught (in promise) TypeError: Object(...) is not a function

How can I use saveFile() from main.js?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source