简体   繁体   English

express.js 应用程序未显示控制台日志消息

[英]express.js app get not showing console log message

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

app.listen(3000, function () {
  console.log("started at 3000");
});

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

  console.log("Hello");

  const car = "read";

  res.send("Hello, world");

});

res.send is working fine. res.send 工作正常。 However, console log is not showing.但是,没有显示控制台日志。 It only shows when I remove the variable.它仅在我删除变量时显示。

I am running using nodemon.我正在使用 nodemon 运行。

Any idea what's going on?知道发生了什么吗?

it works perfectly fine,just to confirm things add app.listen() to the bottom of code.它工作得很好,只是为了确认将 app.listen() 添加到代码底部。

  1. after running nodemon app.js运行nodemon app.js

  2. open browser-> visit localhost:3000 , you will see the hello in your console/cmd/terminal打开visit localhost:3000 > visit localhost:3000 ,您将在控制台/cmd/终端中看到 hello

Reason :- The below mentioned code literally means when a GET request is send to this path ie "/" then, in the console print "Hello", and as a response to that GET request, send "Hello,World"原因:-下面提到的代码字面意思是当 GET 请求发送到此路径时,即“/”,然后在控制台中打印“Hello”,并作为对该 GET 请求的响应,发送“Hello,World”

const port =3000||port.env.PORT

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

  console.log("Hello");

  const car = "read";

  res.send("Hello, world");

});
    app.listen(port, function () {
  console.log("started at "+port);
});

Sometimes nodemon can be a little wacky or late to take in the updates,in such situations, therefore, try stopping the server and rerun it using the nodemon command有时 nodemon 接收更新可能有点古怪或迟到,因此,在这种情况下,请尝试停止服务器并使用 nodemon 命令重新运行它

Your code is working fine.您的代码运行良好。 As good practice.作为良好的做法。 Always put app.listen as the last code in index (entry) file.始终将 app.listen 作为索引(条目)文件中的最后一个代码。

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

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

    console.log("Hello");
  
    const car = "read";
  
    res.send("Hello, world");
  
  });

app.listen(3000, function () {
  console.log("started at 3000");
});

If you're using Postman to send your GET request, the console.log() should show up in the console in vscode or wherever you run your express server from, it will not display in Postman as part of the api response.如果您使用 Postman 发送 GET 请求,console.log() 应显示在 vscode 的控制台中或您运行快速服务器的任何位置,它不会作为 api 响应的一部分显示在 Postman 中。 For your console to log correctly you need to restart your server every time you change your files, so you say you use Nodemon for that.为了让您的控制台正确登录,您每次更改文件时都需要重新启动服务器,因此您说您为此使用了 Nodemon。 Maybe try stopping and restarting your project again manually?也许尝试手动停止并重新启动您的项目? Are you looking for the console.log to display in the right place?您是否正在寻找要在正确位置显示的 console.log?

there is no problem in your code it's absolutely right你的代码没有问题,完全正确

you can see the console.log("hello") in the cmd你可以在cmd中看到console.log("hello")

when you run the localhost:3000 in your browser you can see it at cmd after 'started at 3000'当您在浏览器中运行localhost:3000 ,您可以在“从 3000 开始”之后在 cmd 中看到它

具有自动保存功能的 Nodemon 会导致问题,请改用手动保存 (Ctrl + S)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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