在某种程度上用户必须能够控制节点。这一点通过搭建一个HTTP服务器可以实现。
- var initHttpServer = () => {% w- Z9 Z1 s5 q. k8 W; D1 `6 O
- var app = express();
- app.use(bodyParser.json());6 _9 p% E, w$ Q3 b- b# S7 x
- app.get('/blocks', (req, res) => res.send(JSON.stringify(blockchain)));/ w0 l+ K3 R' a/ E4 v" B) A
- app.post('/mineBlock', (req, res) => {9 }1 o2 E% Y- N
- var newBlock = generateNextBlock(req.body.data);8 f7 K! J# ~; o% X. j8 U9 k- l- ^& T' a
- addBlock(newBlock);
- broadcast(responseLatestMsg());2 O, x9 s# _% o7 ^/ M, k
- console.log('block added: ' + JSON.stringify(newBlock));
- res.send();4 e5 y) \$ _; D( A) G
- });" N8 @; u: [+ T9 e: Q
- app.get('/peers', (req, res) => {6 A% E- G0 p* z6 }" M* R4 T/ L7 `/ A
- res.send(sockets.map(s => s._socket.remoteAddress + ':' + s._socket.remotePort));# C( _5 J4 \9 @' O2 ]$ ?7 W7 {
- });
- app.post('/addPeer', (req, res) => {
- connectToPeers([req.body.peer]);; X, j/ a# @: Q: t$ P' _' l4 p
- res.send();
- });
- app.listen(http_port, () => console.log('Listening http on port: ' + http_port));) ?% R3 N' z/ J: y
- };