快速路由练习返回奇怪的错误

时间:2018-11-18 12:24:02

标签: javascript node.js express

我不知道这种方法总体上是否很干燥,但是我在if语句中寻找有关本练习第二部分的解决方案:

#Express Routing Assignment

1. Create a brand new Express app from scratch
2. Create a package.json using `npm init` and add express as a dependency
3. In your main app.js file, add 3 different routes:

Visiting "/" should print "Hi there, welcome to my assignment!"
==============================================================
Visiting "/speak/pig" should print "The pig says 'Oink'"
Visiting "/speak/cow" should print "The cow says 'Moo'"
Visiting "/speak/dog" should print "The dog says 'Woof Woof!'"
==============================================================
Visiting "/repeat/hello/3" should print "hello hello hello"
Visiting "/repeat/hello/5" should print "hello hello hello hello hello"
Visiting "/repeat/blah/2"  should print "blah blah"

If a user visits any other route, print:
"Sorry, page not found...What are you doing with your life?"

我的第一个解决方案

var express = require("express");
var app = express();

app.get("/", function(req, res){
   res.send("Hi welcome"); 
});

app.get("/speak/:animal", function(req, res){

   var animal = req.toString();

   if (animal.toLowerCase() === "pig") {
   res.send("the pig says oink"); 
   }
   else if (animal.toLowerCase() === "dog") {
   res.send("The dog says woof"); 
   }
   else if (animal.toLowerCase() === "cow") {
   res.send("the cow says moo"); 
   }
});

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Server has started");
}); 

这会在页面上显示以下“错误”

<html>
  <head>
    <meta charset='utf-8'> 
    <title>Error 502 - Bad Gateway</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" />
    <style type="text/css">
    .error_content {
        background: rgba(255, 255, 255, 0.23);
        padding: 10px;
        width: 641px;
        margin: 25px 0;
        display: none;
    }

    #error-msg {
        display: block;
    }
    </style>
  </head>
  <body class="errorUnknown light">
    <div id="wrapper">
      <h1>Error 502 - Bad Gateway</h1>
      <div class="error_content" id="error-msg">
          <p>Please click <a href="javascript:location.reload(true)">here</a> to try again, if the issue persists please contact <a href="https://c9.io/support">support</a></p>
      </div>

      <a href="http://status.c9.io">Status Page</a> |
      <a href="https://c9.io/support">Support</a> |
      <a href="https://c9.io/dashboard.html">Dashboard</a> |
      <a href="https://c9.io">Home</a>
    </div>
  </body>
</html>

这是什么原因导致的?这是服务器端问题吗?当c9关闭时,如何检查我的代码是否正在运行?我真的想“自己解决”这个问题,而不仅仅是查找解决方案。

0 个答案:

没有答案