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 Web Development - Fourth Edition 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.