Blockchain Nanodegree Notes 20181109

Term 1 Session 4 Lesson 1: Planning a Web Service

  • Analyze blockchain use case to determine if it requires a web service
  • Decide on a web service language
  • Explain Node.js framework and components for blockchain

Web Service

A web service is a function that can be accessed by other programs over the web.

Web services vs Websites

  • Websites allow people to communicate and collaborate with each other.
  • Web services allow programs to communicate and collaborate with each other

Frontend vs Backend

在这里插入图片描述

Web Services with Blockchain Considerations

Yes if -

  • Consumers require a user interface to interact with the application
  • Every client (browser) should communicate w/ its own app instance
  • Desire no central server

No if -

  • Certain tasks can be handled without using a web service (eg. Contract execution)
  • Mobile application directly communicates with blockchain platform

Do You Really Need a Blockchain

Problem of Value Identification

  1. Is there a need to share information, credentials or value with others?
  2. Is trust a critical requirement to the process?
  3. Do you need to prove to others you are transacting/reporting accurately?
  4. Is there potential to monetize the data or digital asset in the value chain?
  5. Who owns the problem? Individual or industry wide challenges?

Stakeholders buy in

  1. Is there a network of stakeholders (i.e. more than 2)?
  2. Is there a dependency on others for information?
  3. Does more than one participant need to update the data?
  4. Is there scope to open up the ecosystem to ancillary parties in the future?
  5. Are you working with other industry players on any activities?

Technical considerations

  1. Is there any ongoing need or future requirements for high data throughput?
  2. Do you rely or use public data sources to make decisions?
  3. Do you need to store a particularly rich/complex data structure?
  4. Do you need to digitize assets in your value chain?
  5. Do you need transaction privacy? Do you need anonymity?

Planning Web Services with Our Private Blockchain

  • Configure web service API with GET/POST endpoints
    • Build and deploy local web service with Node.js
    • Configure API endpoints with static mock data for testing
      • GET - Block by ID
      • POST - New Block
  • Migrate your private blockchain to your API web service
    • Configure API endpoints to interact with your private blockchain
      • GET - Block by ID
      • POST -New Block
    • Create a method to validate blockchain health

Web Services with Node.js

Node.js Intro

Web service choices: Haskell, PHP, Python, Java, JavaScript, Ruby

Reasons for JavaScript
  • Popular Developer Language
  • Lots of Existing Tooling
  • Simple, Expressive, Powerful
Deciding on a Web Service Language
  • Are you concerned with frontend, backend or both?
  • How well is the language maintained?
  • How well does this language integrate with other systems?
  • What’s its performance like?

在这里插入图片描述

Resons for Node.js
  • Very memory efficient
  • Scalable
  • Large ecosystem of open-source libraries
  • Cleaner codebase

Node.js Components

在这里插入图片描述

Modules:Related code that is encapsulated into modules. They can be thought of as JavaScript Libraries.

Core modules: the bare minimum Node.js functionalities provided to you.

Third Party Modules: a wrapper for some API functions of third party companies.

package.json A file that lives in the root of your package/application. Tells npm how your package is structured and how to install it. Similar to a manifest in other applications.

Node package manger (NPM): a package manager for Node.js packages or modules.

Node Version Manager (NVM): a tool that allows you to seamlessly switch between different versions of Node. You can install each version with a single command and set a default via the command line interface.

Node Server-Side HTTP Reading from a File (no blockchain data yet)
// Code snippet from https://www.w3schools.com/nodejs/nodejs_http.asp
// Http library
const http = require('http');
// File service library
const fs = require('fs');
// Http port
const port = 3000;
// Filename for file service to load
const filename = "index.html";
// Configure web service
const app = http.createServer(function (request, response){
  response.writeHead(200, {"Content-Type": "text/html"});
  response.end(fs.readFileSync(__dirname + "/" + filename));
});
// Notify console
console.log("Web Server started on port 3000\nhttp://localhost:"+port);
// Start server with http port
app.listen(port);
Code Explanation
  • Instantiate an HTTP server with a request handler function, and have it listen on a port.
  • Get headers, URL, method and body data from request objects.
  • Make routing decisions based on URL and/or other data in request objects.
  • Send headers, HTTP status codes and body data via response objects.
  • Pipe data from request objects and to response objects.
  • Handle stream errors in both the request and response streams.

Node Server Side Using Blockchain Mock Data

// Http library
const http = require('http');
// Http port
const port = 8080;
// Configure web service
const app = http.createServer(function (request, response){
response.writeHead(200, {"Content-Type": "application/json"});
let block = {"height":"0","body":"123"};
response.write(JSON.stringify(block));
response.end();
});
// Notify console
console.log("Web Server started on port 3000\nhttp://localhost:"+port);
// Start server with http port
app.listen(port);

Code Practice

https://github.com/udacity/nd1309_exercise_client_server

Step 1Clone the project repository in this Github repository
Step 2Open the terminal and install the packages: npm install
Step 3Open the file app.js and start coding - See additional info below
Step 4Run your application node app.js
Step 5Go to your browser and type: http://localhost:8080/
Step 6Get the hash generated in your browser and answer the quiz in your classroom

Additional info for Step 3

Review the boilerplate code

Once you have cloned Github project, and installed the packages dependencies with npm install, open the app.js file:

// Http library
const http = require('http');

//Step 1. Import crypto-js/sha256


// Http port
const port = 8080;

//Mock Data
var blocks = [];
let block_1 = {"height":"0","body":"Udacity Blockchain Developer", "time": 1538509789};
let block_2 = {"height":"1","body":"Udacity Blockchain Developer Rock!", "time": 1538509789};
blocks.push(block_1);
blocks.push(block_2);

//Step 2. Configure web service
/**
 * Take the block_2 data from the array "blocks" and generate the hash to be written into the response.
 */
//Add your code here



// Notify console
console.log("Web Server started on port 8080\nhttp://localhost:"+port);
// Start server with http port
app.listen(port);

As you can see the tasks you have to complete are:

  1. Import crypto-js/sha256
  2. Create the variable app assigning the function: const app = http.createServer(function (request, response){ // your code go here}

The problem that you have to solve is add to the response of this service the hash generated from the data of block_2 object that is in the array blocks.

Mock Data Set

Why Use Mock Data? (To test functionality)
  • Block Size
  • Chain Size
  • Performance
  • Security
  • Data Corruption

Testing endpoints

  • Postman
  • Curl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值