简体   繁体   中英

Node.js express console.log doesn't show anything

Sometimes console.log doesn't show messages. In app.listen everything works fine but in app.post - NOT. Please help to figure it out!

Node.js 12.14.1 Express 4.17.1

const express = require("express");
const bodyParser = require("body-parser");
const request = require('request');

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

app.listen(3000, function () {
    console.log("Server is running on 3000 port");
})

app.get("/", function (req, res) {
    res.sendFile(__dirname + "/index.html");
})

app.post('/', function (req, res) {

    console.log("POST triggered!");
    //There is NO any message in my Terminal
    res.send('POST request to homepage');
    //It works properly

})

You're running a nodejs express server, the console is supposed to be the the console of your server, check you Vs Code's Console if you're on VS Code.

Hit localhost:port/ and watch your editor's or cmd's console, from where you started the server.

I am using a windows new terminal and it's a thing. Somehow my server was dis-attached from console and lived on his own, so all messages it was sending to nowhere!

Solution is to kill all node processes!

Both Get and Post Requests are working fine. You should have to Post requests from the postman and get from the browser. Because by default browser request is Get.

Code -

const express = require("express");
const bodyParser = require("body-parser");
// const request = require('request');

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));

app.listen(3000, function () {
  console.log("Server is running on 3000 port");
})

app.get("/", function (req, res) {
  console.log("get triggered!");
  res.sendFile(__dirname + "/index.html");
})

app.post('/', function (req, res) {
  console.log("POST triggered!");
  //There is NO any message in my Terminal
  res.send('POST request to homepage');
  //It works properly

})

邮差 在此处输入图片说明

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