0

Let's say I have a normal mjs file and an API such as this:

// Storage.mjs
class Storage {
    constructor(name, age) {
        this.name = name;
        this.age = age;
        this.json = JSON.stringify(this);
    }
}
var store = new Storage('eMart', 2);

// server.js
var express = require('express'),
    fs = require('fs'),
    data = fs.readFileSync('website/js/storage.mjs'),
    convert = JSON.parse(data);

var app = express(),
    server = app.listen(3000, initialize);

console.log(convert);

function initialize() {
    console.log("Local Host Initialized.");
}

app.use(express.static('website'));

My goal is to send the JSON data to API which is inside of class syntax, but every time Node keeps throwing undefined and an error like this picture;

enter image description here

Most of people's question was sending data from API to specific js file which was totally opposite of my case. It was hard to find solution from there.

Are there any proper ways to pass JSON data to API?

sniffingdoggo
  • 398
  • 4
  • 16
  • There's no data inside of the `mjs` file except 2 (eMart is apparently defined elsewhere). You can't just read a module file and pretend it's JSON. JSON is a specific format for text to encode data (not classes and the like). See http://json.org/ for specifics. – Heretic Monkey Sep 04 '19 at 01:54
  • @HereticMonkey My bad. value eMart is actually a text. – sniffingdoggo Sep 04 '19 at 02:35

1 Answers1

1

I suppose this is what you want?

// assume this is loaded with fs.readFileSync
const mjs = `
class Storage {
    constructor(name, age) {
        this.name = name;
        this.age = age;
        this.json = JSON.stringify(this);
    }
}
var store = new Storage('eMart', 2);  // I assume you missed the quote here
`;

eval(mjs);  // You can't just convert an js file into a JSON, you have to eval it first(not safe, use it with caution)

let result = JSON.parse(store.json);  // Since the file outputs a variable called "store" and it has a "json" property

console.log(result);

The server.js snippet

// server.js
var express = require('express'),
    fs = require('fs'),
    data = fs.readFileSync('website/js/storage.mjs');  

(0, eval)(data);  // indirect eval
var convert = JSON.parse(store.json);

var app = express(),
    server = app.listen(3000, initialize);

console.log(convert);

function initialize() {
    console.log("Local Host Initialized.");
}

app.use(express.static('website'));
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
  • Thank you for answering but Node is still saying an error which is `store is not defind.`. – sniffingdoggo Sep 04 '19 at 02:12
  • 1
    If the eval runs without an error. It probably because your code is running in `use strict` mode so it failed compiling, in that case try to declare it first like above that I just updated. – Hao Wu Sep 04 '19 at 02:18
  • 1
    Sorry, the predefinition seems do not work in `use strict` either. Try to make an `Indirect eval call` to bypass this behavior like the update above, here's the reference: https://stackoverflow.com/questions/19357978/indirect-eval-call-in-strict-mode – Hao Wu Sep 04 '19 at 02:29
  • Got it. Thanks for detailed answering. – sniffingdoggo Sep 04 '19 at 02:31