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}`); });

A+ Guide To It Technical Support
10th Edition
ISBN:9780357108291
Author:ANDREWS, Jean.
Publisher:ANDREWS, Jean.
Chapter18: Macos, Linux, And Scripting
Section: Chapter Questions
Problem 14TC
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
A+ Guide To It Technical Support
A+ Guide To It Technical Support
Computer Science
ISBN:
9780357108291
Author:
ANDREWS, Jean.
Publisher:
Cengage,