首页 > 解决方案 > 即使没有人在做请求,Nodemon 也会继续记录中间件的内容

问题描述

const Joi = require('joi');
const express = require('express');
const app = express();

app.use(express.json());

app.use(function(req, res, next) {
  console.log("Logging...")
  next();
})
app.use(function(req, res, next) {
  console.log("Authenticated...")
  next();
})


const courses = [
  {
    id: 1,
    name: 'JavaScript',
    description: 'JavaScript is a high-level, dynamic, untyped, and interpreted programming language.'
  },
  {
    id: 2,
    name: 'Node.js',
    description: 'Node.js is a JavaScript runtime built on Chrome\'s V8 JavaScript engine.'
  },
  {
    id: 3,
    name: 'Express',
    description: 'Express is a minimal and flexible Node.js web application framework.'
  }
];

app.get('/', (req, res) => {
    res.send('Hello World');
})

//every req,res callback it's a middleware
app.get('/api/courses', (req, res) => {
   res.send(courses);
})

app.get('/api/courses/:id', (req, res) => {
  const course = courses.find(course => course.id == req.params.id)
  if(!course) {
    res.status(404).send('Course not found');
  }
  else {
    res.send(course);
  }
})

app.post('/api/courses', (req, res) => {
   //for this to work we need to enabling parsing the body of the request
   const schema = Joi.object({
     name: Joi.string().required(),
     description: Joi.string().required()
   });
   const result = schema.validate(req.body);

   if(result.error) {
      res.status(400).send(result.error);
      return
   }
   else {
  const course = {
     id: courses.length + 1,
     name: req.body.name,
     description: req.body.description
  }
  courses.push(course);
  //this sends the course object back to the client in the body of the response
  //(which is the standard way to do this)
  res.send(course);
}})

app.put('/api/courses/:id', (req, res) => {
   const course = courses.find(course => course.id == req.params.id)
   if(!course) {
     res.status(404).send('Course not found');
   }
   else if (!req.body.name && !req.body.description) {
      res.status(404).send('Wrong property bitch')
   }
   else {
     course.name = req.body.name ?? course.name;
     course.description = req.body.description ?? course.description;
     res.send(course);
   }
})

app.delete('/api/courses/:id', (req, res) => {
   const course = courses.find(course => course.id == req.params.id)
   if(!course) {
     res.status(404).send('Course not found');
   }
   else {
     courses.splice(courses.indexOf(course), 1);
     res.send('Course deleted');
   }
})


app.listen(3000, () => {
console.log('Example app listening on port 3000!');
})

它一遍又一遍地记录这个

记录... 已验证... 记录... 已验证... 记录... 已验证... 记录... 已验证... 记录... 已验证... 记录... 已验证... 记录。 .. 已通过身份验证... 正在记录... 已通过身份验证... 正在记录...

这是 package.json

{
  "name": "nodefundamentals",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "nodemon app",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "joi": "^17.4.2"
  }
}

package.json 和 app.js 文件是我仅有的两个。我该如何解决这个问题?

我觉得我错过了一些东西,但我逐行遵循教程,我已经得到这个递归日志有一段时间了。

标签: node.jsexpresspackage.json

解决方案


请更换??与 ||。

而不是 else if 使用 if return 我在这里做了一个固定的 repl

索引代码

    const Joi = require('joi');
const express = require('express');
const app = express();

app.use(express.json());

app.use(function(req, res, next) {
  console.log("Logging...")
  next();
})
app.use(function(req, res, next) {
  console.log("Authenticated...")
  next();
})


const courses = [
  {
    id: 1,
    name: 'JavaScript',
    description: 'JavaScript is a high-level, dynamic, untyped, and interpreted programming language.'
  },
  {
    id: 2,
    name: 'Node.js',
    description: 'Node.js is a JavaScript runtime built on Chrome\'s V8 JavaScript engine.'
  },
  {
    id: 3,
    name: 'Express',
    description: 'Express is a minimal and flexible Node.js web application framework.'
  }
];

app.get('/', (req, res) => {
    res.send('Hello World');
})

//every req,res callback it's a middleware
app.get('/api/courses', (req, res) => {
   res.send(courses);
})

app.get('/api/courses/:id', (req, res) => {
  const course = courses.find(course => course.id == req.params.id)
  if(!course) {
    res.status(404).send('Course not found');
  }
  else {
    res.send(course);
  }
})

app.post('/api/courses', (req, res) => {
   //for this to work we need to enabling parsing the body of the request
   const schema = Joi.object({
     name: Joi.string().required(),
     description: Joi.string().required()
   });
   const result = schema.validate(req.body);

   if(result.error) {
      res.status(400).send(result.error);
      return
   }
   else {
  const course = {
     id: courses.length + 1,
     name: req.body.name,
     description: req.body.description
  }
  courses.push(course);
  //this sends the course object back to the client in the body of the response
  //(which is the standard way to do this)
  res.send(course);
}})

app.put('/api/courses/:id', (req, res) => {
   const course = courses.find(course => course.id == req.params.id)
   if(!course) return res.status(404).send('Course not found');
   
   if (!req.body.name && !req.body.description )return res.status(404).send('Wrong property bitch')
   

     course.name = req.body.name || course.name;
     course.description = req.body.description || course.description;
     res.send(course);

})

app.delete('/api/courses/:id', (req, res) => {
   const course = courses.find(course => course.id == req.params.id)
   if(!course) return res.status(404).send('Course not found');
   
     courses.splice(courses.indexOf(course), 1);
     res.send('Course deleted');
   
})


app.listen(3000, () => {
console.log('Example app listening on port 3000!');
})

推荐阅读