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
- Is there a need to share information, credentials or value with others?
- Is trust a critical requirement to the process?
- Do you need to prove to others you are transacting/reporting accurately?
- Is there potential to monetize the data or digital asset in the value chain?
- Who owns the problem? Individual or industry wide challenges?
Stakeholders buy in
- Is there a network of stakeholders (i.e. more than 2)?
- Is there a dependency on others for information?
- Does more than one participant need to update the data?
- Is there scope to open up the ecosystem to ancillary parties in the future?
- Are you working with other industry players on any activities?
Technical considerations
- Is there any ongoing need or future requirements for high data throughput?
- Do you rely or use public data sources to make decisions?
- Do you need to store a particularly rich/complex data structure?
- Do you need to digitize assets in your value chain?
- 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
- Configure API endpoints to interact with your private blockchain
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 1 | Clone the project repository in this Github repository |
---|---|
Step 2 | Open the terminal and install the packages: npm install |
Step 3 | Open the file app.js and start coding - See additional info below |
Step 4 | Run your application node app.js |
Step 5 | Go to your browser and type: http://localhost:8080/ |
Step 6 | Get 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:
- Import crypto-js/sha256
- 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