The node server is not working when I trying to run it. It says Cannot GET /. How to fix it? const express = require('express'); const dotenv = require('dotenv'); dotenv.config(); const app = express(); const port = process.env.PORT || 3000; const host = process.env.HOST || 'localhost'; app.get('/dotted', (req, res) => { const { word1, word2 } = req.query; const dots = '.'.repeat(30 - (word1.length + word2.length)); const result = `${word1}${dots}${word2}`; res.type('html').send(result); }); app.get('/fizzBuzz', (req, res) => { const { start, end } = req.query; let result = ''; for (let i = start; i <= end; i++) { if (i % 3 === 0 && i % 5 === 0) { result += 'FizzBuzz\n'; } else if (i % 3 === 0) { result += 'Fizz\n'; } else if (i % 5 === 0) { result += 'Buzz\n'; } else { result += `${i}\n`; } } res.type('html').send(`${result.trim()}`); }); app.get('/gradeStats', (req, res) => { const grades = req.query.grades; const min = Math.min(...grades); const max = Math.max(...grades); const avg = (grades.reduce((acc, grade) => acc + grade) / grades.length).toFixed(2); res.json({ average: avg, minimum: min, maximum: max }); }); app.get('/rectangle', (req, res) => { const { length, width } = req.query; const area = length * width; const perimeter = 2 * (length + width); res.json({ area: area, perimeter: perimeter }); }); app.listen(port, host, () => { console.log(`Server listening at http://${host}:${port}`); });

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

The node server is not working when I trying to run it. It says Cannot GET /. How to fix it?

const express = require('express');
const dotenv = require('dotenv');

dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';

app.get('/dotted', (req, res) => {
  const { word1, word2 } = req.query;
  const dots = '.'.repeat(30 - (word1.length + word2.length));
  const result = `<pre>${word1}${dots}${word2}</pre>`;

  res.type('html').send(result);
});

app.get('/fizzBuzz', (req, res) => {
  const { start, end } = req.query;
  let result = '';

  for (let i = start; i <= end; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
      result += 'FizzBuzz\n';
    } else if (i % 3 === 0) {
      result += 'Fizz\n';
    } else if (i % 5 === 0) {
      result += 'Buzz\n';
    } else {
      result += `${i}\n`;
    }
  }

  res.type('html').send(`<pre>${result.trim()}</pre>`);
});

app.get('/gradeStats', (req, res) => {
  const grades = req.query.grades;
  const min = Math.min(...grades);
  const max = Math.max(...grades);
  const avg = (grades.reduce((acc, grade) => acc + grade) / grades.length).toFixed(2);

  res.json({ average: avg, minimum: min, maximum: max });
});

app.get('/rectangle', (req, res) => {
  const { length, width } = req.query;
  const area = length * width;
  const perimeter = 2 * (length + width);

  res.json({ area: area, perimeter: perimeter });
});

app.listen(port, host, () => {
  console.log(`Server listening at http://${host}:${port}`);
});

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education