简体   繁体   中英

node.js - home page doesn't load but other pages do

I'm learning node.js. This code is from a class. It has if else statements. The point is to simplify it and one of the ways I'm doing is using switch case . I'm not sure why the home page doesn't load but "other page" and other other page" does.

As a secondary problem, does figlet work on windows? I use npm install figlet in my node_modules folder. Not sure if I can post multiple problems in one thread. If not, I would like to have my home page solved first.

const http = require("http");
const fs = require("fs");
const url = require("url");
const querystring = require("querystring");
// const figlet = require("figlet");

const server = http.createServer((req, res) => {
  const page = url.parse(req.url).pathname;
  const params = querystring.parse(url.parse(req.url).query);
  console.log(page);

  switch (page) {
    case "/":
      fs.readFile("index.html", function (err, data) {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.write(data);
        res.end();
      });
      break;
    case "/otherpage":
      fs.readFile("otherpage.html", function (err, data) {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.write(data);
        res.end();
      });
      break;
    case "/otherotherpage":
      fs.readFile("otherotherpage.html", function (err, data) {
        res.writeHead(200, { "Content-Type": "text/html" });
        res.write(data);
        res.end();
      });
      break;
    case "/api":
      if ("student" in params) {
        if (params["student"] == "leon") {
          res.writeHead(200, { "Content-Type": "application/json" });
          const objToJson = {
            name: "leon",
            status: "Boss Man",
            currentOccupation: "Baller",
          };
          res.end(JSON.stringify(objToJson));
        } else if (params["student"] != "leon") {
          res.writeHead(200, { "Content-Type": "application/json" });
          const objToJson = {
            name: "unknown",
            status: "unknown",
            currentOccupation: "unknown",
          };
          res.end(JSON.stringify(objToJson));
        }
      }
      break;
    case "/ccs/style.css":
      fs.readFile("css/style.css", function (err, data) {
        res.write(data);
        res.end();
      });
      break;
    case "/js/main.js":
      fs.readFile("js/main.js", function (err, data) {
        res.writeHead(200, { "Content-Type": "text/javascript" });
        res.write(data);
        res.end();
      });
      break;
    default:
      figlet("404!!", function (err, data) {
        if (err) {
          console.log("Something went wrong...");
          console.dir(err);
          return;
        }
        res.write(data);
        res.end();
      });
  }
});

server.listen(8000);

Did you tried using empty string "" instead of "/" in your case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM