Creating and launching a service

We will implement an express (https://expressjs.com/) web application in Node.js to expose the preceding API. The code lies in the application folder in our repository, with the source code in app.js and the dependencies defined in package.json. As a prerequisite to running the web application, the dependencies must be installed either by running npm install or make (see Chapter 8, Agility In A blockchain network) within that folder.

The following code snippet shows how to instantiate and run the express server:

var express = require('express'),
var bodyParser = require('body-parser'),
var app = express();
var port = process.env.PORT || 4000;
app.options('*', cors());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
var server = http.createServer(app).listen(port, function() {});

To summarize, a web server is launched to listen for HTTP requests on port 4000. Middleware is configured to enable CORS, automatically parsing both JSON payloads and forming data in POST requests.

Our web server listens to requests over an insecure HTTP. In a production application, we would start an HTTPS server for secure, confidential communication with clients.

Now, let's see how to configure the various express routes to implement our service API functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.149.19