Using Express.js

Using Express is pretty straightforward: lets see how to use it: 

  1. Simply copy the example code that is present in the documentation and paste it into our api.js file:
var express = require('express')var app = express() app.get('/', function (req, res) { res.send('Hello World')}) app.listen(3000)

As you can see, at the top of our file, we are requiring express, the library that we just downloaded, and then we are creating an app. This app will help us handle different endpoints or different routes.

For example, we have a get endpoint, which is just /. With this endpoint, we are sending back the response of Hello World. This whole server is listening on port 3000.

  1. To start this server, we go over to our terminal and run ...

Get Learn Blockchain Programming with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.