I'm setting up a blockchain client-server network with NodeJS and ExpressJS. I have the server running successfully and the blockchain functioning by itself. But I'm having some problems combining the two. Firstly, here is my project structure:
node_p2p/
node_modules/
...
public/
block.js
client.js
index.html
package-lock.json
package.json
server.js
The actual problem that I have is, I want to include block.js
in the client.js
file, but I always get ReferenceError: require is not defined
on the line, where I include block.js
into client.js
:
var block = require('./block.js');
I have already tried the following variations, but all of them produce the same error (quite expected, since require directory is relative to the file calling require, but I tried them anyway, just in case):
var block = require('../public/block.js');
var block = require('../public/block');
var block = require('./public/block.js');
var block = require('./public/block');
var block = require('/public/block.js');
var block = require('/public/block');
var block = require('../block.js');
var block = require('../block');
var block = require('./block.js');
var block = require('./block');
var block = require('block.js');
var block = require('block');
Here's the code of block.js
:
const SHA256 = require ('crypto-js/sha256');
class Block{
constructor(...){...}
...
}
class Blockhain{
constructor(...){...}
...
}
module.exports = {
Blockchain: function(){
return new Blockchain();
}
}
Secondly, client.js
:
var socket = io.connect('http://localhost:3000');
var block = require('./block.js');
socket.on('client', showData);
socket.on('clientno', showData);
socket.on('newclient', showData);
function showData(data) {
console.log(data);
}
var nascoin = new block.Blockchain();
And finally, server.js
:
var express = require('express');
var app = express();
var server = app.listen(3000);
app.use(express.static('public'));
console.log("server running!");
var socket = require('socket.io');
var io = socket(server);
io.sockets.on('connection', newConnection);
var client_counter = 0;
function newConnection(socket) {
console.log('new conn: ' + socket.id);
client_counter++;
socket.emit('client', 'your client name: ' + socket.id);
socket.emit('clientno', 'you are client no.: ' + client_counter);
socket.broadcast.emit('newclient', 'new client has joined the fray');
socket.on('server', function(data) {
console.log(data);
});
}
I don't even know what else I can do, either I'm just that dumb or that blind...