Metamask popup on Server side Express js

+1 vote
So I have this problem when i used "window.web3.currentProvider" on my controller in express js, which i got an error say "window is undefined". And i know the "window" is not to work on server side.

Here is my code : (to post upload file), that works just fine, but i need my Metamask to popup so it can confirm the transaction and im still using TESTRPC. So is there any solution for me? Thank you~

exports.postUploadFile = (req, res, next) => {

var ethereumURI = req.body.ethereumURI;

var abi = JSON.parse(req.body.abi);

var contractAddress = req.body.contractAddress;

let web3 = new Web3();

web3.setProvider(new web3.providers.HttpProvider(ethereumURI));

//define default account[0]

const address = web3.eth.accounts[0];

const fileTitle = req.body.fileTitle;

const description = req.body.description;

const file = req.file;

const fileUrl = file.path;

const fileName = file.filename;

const submitAt = moment(new Date(Date.now())).format('DD-MM-YYYY, HH:mm:ss');

const newReportFile = fs.readFileSync(req.file.path);

const encodingBase64 = newReportFile.toString('base64');

const encodingMD5 = crypto.createHash('md5').update(newReportFile).digest('HEX');

if (!web3.isConnected()) {

throw new Error('unable to connect to ethereum node at ' + ethereumURI);

} else {

console.log('connected to ethereum node at ' + ethereumURI);

let accounts = web3.eth.accounts[0];

console.log('account : ' + accounts);

}

var DFRVerificationContract = web3.eth.contract(abi);

var Report = DFRVerificationContract.at(contractAddress);

console.log('Report has been sent!');

const bapFiles = new BapFiles({

fileName: fileName,

submitAt: submitAt,

fileTitle: fileTitle,

description: description,

fileUrl: fileUrl,

userId: req.user

});

bapFiles.reportFile.data = encodingBase64;

bapFiles.reportFile.contentType = req.file.mimetype;

bapFiles.reportFileHash = encodingMD5;

if (req.session.isLoggedIn) {

bapFiles.save().then(result => {

console.log(result);

var id = result._id;

console.log('ID:', id);

var fileReport = {

filePath: fileUrl,

fileName: fileName,

fileHash: encodingMD5,

description: description,

createdAt: submitAt

};

var reportObject = JSON.stringify(fileReport);

var reportWithoutQuotes = reportObject.toString().replace(/"/g, "");

var idObject = JSON.stringify(id);

var IDWithoutQuotes = idObject.toString().replace(/"/g, "");

console.log(fileTitle);

console.log(reportWithoutQuotes);

console.log('address: ', address);

console.log(Report);

Report.uploadReport(IDWithoutQuotes, fileTitle, reportWithoutQuotes, address, {

from: address,

gas: '4700000'

});

console.log('Success Upload File!');

res.redirect('/user/list-upload');

}).catch(err => {

console.log(err);

});

} else {

console.log('Fail to Upload File');

console.log('User not Logged In. Please Login first!');

return res.redirect('/');

}

}
Jul 15, 2019 in Blockchain by Dzaky
• 130 points
1,382 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Blockchain

0 votes
1 answer

TypeError: XMLHttpRequest is not a function when using web3 on meteor server side

I had the same problem. Instead ethereum:web3 Meteor package ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,210 points
844 views
+1 vote
1 answer

Do we need metamask extension to make transactions on ether?

Actually, we don't need Metamask. To make ...READ MORE

answered Apr 8, 2018 in Blockchain by Perry
• 17,100 points
1,043 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,350 points
3,656 views
0 votes
1 answer
0 votes
1 answer

How can I create an online wallet without any 3rd party API on my PHP server?

With any approach, you need a bitcoin ...READ MORE

answered Aug 30, 2018 in Blockchain by Perry
• 17,100 points
2,410 views
0 votes
1 answer

How to create an asset on Bigchaindb server using http call

You could start like this: method = POST URL ...READ MORE

answered Sep 19, 2018 in Blockchain by Christine
• 15,790 points
1,117 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,655 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,211 views
0 votes
0 answers

Error: Unexpected token o in JSON at position 1

I have been working with Interacting a ...READ MORE

Mar 6, 2019 in Blockchain by saeedi
• 120 points

edited Mar 6, 2019 by Omkar 2,173 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP