节点服务器本地主机问题

时间:2020-06-22 13:02:09

标签: javascript node.js express

我正在尝试测试一个简单的javascript文件,但无法测试,因为该页面在没有任何警告的情况下将永久加载,并且在底部显示一个文本栏,显示“正在等待本地主机...” < / strong> 在bash上,我输入节点app.js,然后按Enter键,按预期输入“服务器已启动” ,但是当我进入“ http:// localhost:3000” 页面始终保持永久加载。 我已安装节点并正确表达。 (由于我不知道该怎么做,因此我无法在其中加入快递。) 很好,我是开发领域的新手。

// Express Setup

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


// Setting Up Routes

app.get("/",function(req,res) {  
    res.send = "Hi There, Welcome To My Assignement!";
});


app.get("/:actionName/pig", function(req , res){
    res.send = "The Pig Says \"Oink\" !";
});

app.get("/:actionName/dog", function(req , res){
    res.send = "The Dog Says \"Woof\" !";
});

app.get("/:actionName/cow", function(req , res){
    res.send = "The cow Says \"Moo\" !";
});


app.get("/repeat/:string/:times",function(req,res){
    let times = parseInt(app.param.times);
    for (let i = 0 ; i <= times ; i++) {
        res.send = app.param.toString;
    }
});

app.get("*" , function(req,res){
    res.send = "Error 404, Page not found!";
});


// Setting Port Up

app.listen(3000, function () {
  console.log("Server Has Started!");
});

2 个答案:

答案 0 :(得分:4)

您不应覆盖res.send,这是您应使用要发送给用户的值调用的函数。

例如,您的根路由应如下所示:

app.get("/", function(req,res) {  
    res.send("Hi There, Welcome To My Assignement!");
});

答案 1 :(得分:0)

要在浏览器上运行,您需要告诉您的应用监听某些特定端口:

我在以下代码段中添加了您提供的代码:

app.listen(port, () => {
  console.log(`Listening to requests on http://localhost:${port}`);
});

也不应分配res.send,而是将响应传递给显示

检查此REPL:

Express App