/nodejs-express-mongoose-ethereum-base

A basic Node.js Express API using Mongo and the Ethereum Blockchain

Primary LanguageJavaScript

Node.js-Express-Mongoose-Ethereum Base

Goal

Basic Node.js Express backend API, with MongoDB database and Ethereum blockchain.

Disclaimer: this is not a DApp per se. Actually it's more of a 'CApp' (Centralized App), as the intelligence and the users' private keys are stored in the server. The Ethereum Blockchain is used to validate and store transactions, but there is no smart contract capabilities.

Get started

  1. Prerequisites: You need to have Git, Node/npm, MongoDB and Ganache installed on your computer

  2. Clone the repo

  3. Install npm dependencies (package.json)

npm install
  1. Run MongoDB in the background (in another terminal)
sudo service mongod start
  1. Run Ganache (GUI / CLI)

  2. Run the server. It will be available at http://localhost:3000

npm run start

More details

Tech Stack

  • Node.js + Express + Mongoose + Web3.js
  • MongoDB database
  • Blockchain: Ganache (Ethereum Local Testnet)
  • Authentication using JWT (pass the token in the header x-access-token)
  • Tests with Mocha & Chai
  • Linting with ESLint (Airbnb Style Guide + some custom rules)

API Available

Login

  • POST /api/v1/auth/login

Users

  • GET /api/v1/users
  • POST /api/v1/users
  • GET /api/v1/users/:user_id
  • PUT /api/v1/users/:user_id
  • DELETE /api/v1/users/:user_id

Transactions

  • // GET /api/v1/transactions
  • GET /api/v1/transactions/:transaction_id

User Transactions

  • GET /api/v1/users/:user_id/transactions
  • POST /api/v1/users/:user_id/transactions

Address Transactions

  • GET /api/v1/addresses/:address_id/transactions

Block Transactions

  • GET /api/v1/blocks/:block_number_or_hash

Tip: you can interact with the API with a tool like Postman

Seed Database

node db/seed.js

Run Tests

npm run test

Run Linting

npm run lint

Interact with the Local Ethereum Blockchain directly

node scripts/ethereum_scriptsjs <command>

// command: accounts, balance, unlock, send, transaction, account_transactions, block
// (+ additional parameters needed in some cases)

Things missing & futur improvements

  • Encrypt User.ethereum_account_private_key in database
  • Authorization layer: can only update your data, can only view your transactions, etc
  • Integration Tests with Ethereum/Ganache
  • Unit Tests
  • Mockgoose in memory database for tests
  • REPL to interact with the database using the models