2

I'm taking an online course on writing block-chain from scratch. The course utilizes javascript and node.js. I am very new to these technologies but followed the course so far to the T. i'm pasting the relevant code - the app file (index):

const express = require('express');
const bodyParser = require('body-parser');
const Blockchain = require('../blockchain');
const P2pServer = require('./p2p-server');

const HTTP_PORT = process.env.HTTP_PORT || 3001;

const app = express();
const bc = new Blockchain();
const p2pServer = new P2pServer(bc);

app.use(bodyParser.json());

app.get('/blocks', (req, res) => {
    res.json(bc.chain);
});

app.post('/mine', (req, res) => {
    const block = bc.addBlock(req.body.data);
    console.log(`New blovk added: ${block.toString()}`);

    res.redirect('/blocks');
});

app.listen(HTTP_PORT, () => console.log(`Listening on port ${HTTP_PORT}`));
p2pServer.listen();

and the code from p2p-server.js:

const Websocket = require('ws');

const P2P_PORT = process.env.P2P_PORT || 5001;
const peers = process.env.PEERS ? process.env.PEERS.split(',') : [];
//HTTP_PORT=3002 P2P_PORT=5003 PEERS=ws://localhost:5001 npm run dev

class P2pServer {
    constructor(blockchain) {
        this.blockchain = blockchain;
        this.sockets = [];
    }

    listen() {
        const server = new Websocket.Server({ port: P2P_PORT });
        server.on('connection', socket =>this.connectSocket(socket));

        this.connectToPeers();

        console.log(`listening to peer-to-peer connections on: ${P2P_PORT}`);
    }

    connectToPeers() {
        peers.forEach(peer => {
            const socket = new Websocket(peer);

            socket.on('open', () => this.connectSocket(socket));
        });
    }

    connectSocket(socket){
        this.sockets.push(socket);
        console.log('socket connected');
    }
}

module.exports = P2pServer;

when I try to run the following in the command line:

HTTP_PORT=3002 P2P_PORT=5003 PEERS=ws://localhost:5001 npm run dev

I get the following:

'HTTP_PORT' is not recognized as an internal or external command, operable program or batch file.

for some reason the process.env isn't picking up the input and passing it on to the app. What is wrong here? Thanks!

EDIT: I was asked to add the package.json:

{
  "name": "sf-chain",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --watchAll",
    "dev-test": "nodemon dev-test",
    "start": "node ./app",
    "dev": "nodemon ./app"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^23.1.0",
    "nodemon": "^1.17.5"
  },
  "dependencies": {
    "body-parser": "^1.18.3",
    "crypto-js": "^3.1.9-1",
    "express": "^4.16.3",
    "ws": "^5.2.0"
  }
}
4
  • You're receiving the error because this is really not a command. You should use just npm run dev. Please, show your package.json file
    – reisdev
    Commented Jun 6, 2018 at 16:04
  • @MatheusReis i added the package.json code. That being said - the command is the exact same command the instructor uses in the course..
    – Kippi
    Commented Jun 6, 2018 at 16:42
  • 1
    I guest you use windows and it doesn't support to set ENV in command line that way
    – nguyenkha
    Commented Jun 6, 2018 at 17:29
  • Oh! I didn't stop to think about that. wow. Need to figure this out.
    – Kippi
    Commented Jun 6, 2018 at 17:40

2 Answers 2

6

You are using:

$ HTTP_PORT=3002 P2P_PORT=5003 PEERS=ws://localhost:5001 npm run dev

It is showing an error, because the window powershell will not recognize this command..

Instead you should use:

set HTTP_PORT=3002 && set P2P_PORT=5002 && set PEERS=ws://localhost:5001 && npm run dev
1

I believe were doing the same course. I'm sure you've probably figured this out but I tried the same step in Git Bash instead of Powershell and it worked.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.