关于 nodemon

关于 nodemon

相关代码:

const http = require("http");

const server = http.createServer((req, res) => {
  const url = req.url;
  const path = url.split("?")[0];
  const method = req.method;
  // console.log("url is:", url);
  // console.log("method is:", method);
  if (path === "/api/list" && method === "GET") {
    res.end("this is list");
  }
  if (path === "/api/create" && method === "POST") {
    res.end("this is create");
  }
  res.end("404");
});
server.listen(3000);

相关代码:

{
  "name": "hello",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev":"nodemon index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

相关截图:

https://img1.sycdn.imooc.com//climg/6190cbd909b36b3d07920673.jpg

问题描述:

老师你好,我这边重新运行后,就会出现这个错误,请问这是怎么回事

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕然然 2021-11-14 17:16:35

同学你好,报错原因如下:

res.end()方法在if语句中执行一次之后,又会执行最后的res.end("404"),res.end()方法不可以重复执行,所以报错。

建议参考以下代码修改一下即可,如图

https://img1.sycdn.imooc.com//climg/6190d3a109a8401507910422.jpg

祝学习愉快!

  • 为啥视频里老师重复使用res.end而没有报错呢?版本的原因吗?

    2022-06-22 18:59:16
  • 就是因为node版本不同,老师安装的是12版本,该版本不会报错,猜测同学的版本比老师高,所以会报错。可以参考上图给出的方案来解决这个报错。

    祝学习愉快~

    2022-06-23 10:01:28
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师