I have a Node.js Express project with the files oriented like this (WebStorm IDE):
I would like to pass a variable from the marked index.js
to app.js
so that I can write that variable in data.json
at the end.
I am still new to Node.js and still confused about the client/server theory. It will be much easier if there is a way to write the data directly from index.js
to data.json
, or any other json file, but I think that this is not possible according to previous answers. Please correct me if I'm wrong.
Update:
Issue was solved using Ajax as mentioned in this answer :
using $.post('/email', { address: 'xxx@example.com' });
in index.js to send data, and
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/email', (req, res) => {
// you have address available in req.body:
console.log(req.body.address);
// always send a response:
res.json({ ok: true });
});
app.use(express.static(dir));
app.listen(4443, () => console.log('Listening on http://localhost:4443/'));
in app.js to recieve data.