在某种程度上用户必须能够控制节点。这一点通过搭建一个HTTP服务器可以实现。# q' U4 y X. X
- var initHttpServer = () => {
- var app = express();* v6 z9 G2 L4 q O2 ~8 v$ _. u
- app.use(bodyParser.json());; V+ x" V% _5 c
- app.get('/blocks', (req, res) => res.send(JSON.stringify(blockchain)));
- app.post('/mineBlock', (req, res) => {
- var newBlock = generateNextBlock(req.body.data);/ m. z9 H' ~) ?: }5 `4 z
- addBlock(newBlock);$ o: x1 m% h6 G: x' p
- broadcast(responseLatestMsg());) P1 j7 k: t+ g Z# _# D" R+ }9 g
- console.log('block added: ' + JSON.stringify(newBlock));
- res.send();* C3 n) g5 w, S( k
- });
- app.get('/peers', (req, res) => {
- res.send(sockets.map(s => s._socket.remoteAddress + ':' + s._socket.remotePort));" L+ o' M# Z/ b; `) W
- });! }: b* F7 d4 }; {& v
- app.post('/addPeer', (req, res) => {
- connectToPeers([req.body.peer]);: v0 k3 j" x! Z7 s* V, U% B
- res.send();
- });
- app.listen(http_port, () => console.log('Listening http on port: ' + http_port));
- };