Refactoring the Fibonacci application for REST

Now that we've implemented a REST-based server, we can return to the Fibonacci application, applying what we've learned to improve it. We will lift some of the code from fiboclient.js and transplant it into the application to do this. Create a new file, routes/fibonacci-rest.js, with the following code:

const express = require('express');const router = express.Router();const http = require('http');const math = require('../math');router.get('/', function(req, res, next) {  if (req.query.fibonum) {    var httpreq = http.request({       host: "localhost",       port: process.env.SERVERPORT,       path: "/fibonacci/"+Math.floor(req.query.fibonum),       method: 'GET'     });    httpreq.on('response', response => { response.on('data', ...

Get Node.js Complete Reference Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.