我正在尝试测试一个简单的javascript文件,但无法测试,因为在我的浏览器上,页面会永远加载,没有任何警告,并且在底部出现一个文本栏,上面写着“Waiting for localhost.。。” 在bash上,我输入node app.js,在按enter后,terminal会显示“Server Has Started”(服务器已启动),这是预期的,但当我转到“http://localhost:3000”时,页面会一直加载下去。 我正确地安装了node和express。 (我无法在此处包含express,因为我不知道如何包含express.) 请客气点,我是新来的发展世界。
null
// 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!");
});
null
您不应该重写res.send
,它是一个函数,您应该用要发送给用户的值调用它。
例如,根路由应如下所示:
app.get("/", function(req,res) {
res.send("Hi There, Welcome To My Assignement!");
});
为了在浏览器上运行,您需要告诉您的应用程序侦听一些特定的端口:
我在您提供的代码中添加了以下代码段:
app.listen(port, () => {
console.log(`Listening to requests on http://localhost:${port}`);
});
同样,不应分配Res.Send,而是传递要显示的响应
检查此REPL:
Express App