I am new to node.js. I have an application angular front end and node.js server. And there is a js library that I am trying to get data.
What I am trying to do is user will press a button and pass data to server. Server will pass data to a node.js library and library will return data to server. Server will send back data to user.
I tried to call js library from angular front end as a script. But there are lots of errors because of webpack is higher than 5.
node.js file:
const Mam = require('@iota/mam')
const { asciiToTrytes, trytesToAscii } = require('@iota/converter')
const mode = 'private'
const provider = 'https://nodes.devnet.iota.org'
const sidekey= 'hf8685nfhfhjs9h8'
let mamState = Mam.init(provider)
const checkMam = async root=>{
const result = await Mam.fetch(root, mode)
result.messages.forEach(message => console.log('Fetched and parsed', JSON.parse(trytesToAscii(message)), '\n'))
return result.messages;
}
module.exports = { checkMam };
server.js file:
const express = require('express');
const MAM_listen = require("./MAM_listen");
const app = express(),
bodyParser = require("body-parser");
port = 3080;
const users = [];
const root ='YQKXWPSBRHZXEMDUMGEHVYJKSKZVNCIBEDPYMURKLRIOOYHBCRTGGOSFGPOVSHCXIXMKKSFJIAUL9XPJZ';
app.use(bodyParser.json());
app.use(express.static(process.cwd()+"/my-app/dist/angular-nodejs-example/"));
app.get('/api/users', (req, res) => {
MAM_listen.checkMam(root).then((value) => console.log(value))
res.json(users);
});
app.post('/api/user', (req, res) => {
const user = req.body.user;
users.push(user);
res.json("user add");
});
app.get('/', (req,res) => {
res.sendFile(process.cwd()+"/my-app/dist/angular-nodejs-example/index.html")
});
app.listen(port, () => {
console.log(`Server listening on the port::${port}`);
});
I am getting this error when I am call (MAM_listen.checkMam) from app.get because the function is promise type. Is it another way to do what I am trying to do? How is this normaly done? Thank you