微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须为字符串类型接收函数wrapedCwd

如何解决TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须为字符串类型接收函数wrapedCwd

这是我的代码

   //Require modules
var http = require("http");
var url = require("url");
var path = require("path");
var fs = require("fs");

var mimeTypes = {
  html: "text/html",jpeg: "image/jpeg",png: "image/png",jpg: "image/jpg",js: "text/javascript",css: "text/css",};

http
  .createServer(function (req,res) {
    var uri = url.parse(req.url).pathname;
    var fileName = path.join(process.cwd,unescape(uri));
    console.log("Loading " + uri);
    var stats;

    try {
      stats = fs.lstatSync(fileName);
    } catch (error) {
      res.writeHead(404,{ "Content-type": "text/plain" });
      res.write("404 not Found");
      res.end();
      return;
    }
    if (stats.isFile()) {
      var mimeType = mimeTypes[path.extname(fileName).split(".").reverse()[0]];
      res.writeHead(200,{ "Content-type": mimeType });

      var fileStream = fs.createReadStream(fileName);
      fileStream.pipe(res);
    } else if (stats.isDirectory) {
      res.writeHead(302,{ location: "index.html" });
      res.end();
    } else {
      res.writeHead(500,{ "Content-Type": "text/plain" });
      res.write("500 Internal Error");
      res.end();
    }
  })
  .listen(3000);

这是在终端中查看的错误

> simpleserver@1.0.0 start C:\Users\User\Desktop\Codes\html workspace\node_temp\simpleserver
> node server.js

internal/validators.js:120
    throw new ERR_INVALID_ARG_TYPE(name,'string',value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received function wrappedCwd
    at validateString (internal/validators.js:120:11)
    at Object.join (path.js:375:7)
    at Server.<anonymous> (C:\Users\User\Desktop\Codes\html workspace\node_temp\simpleserver\server.js:20:25)
    at Server.emit (events.js:315:20)
    at parserOnIncoming (_http_server.js:790:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17) {
  code: 'ERR_INVALID_ARG_TYPE'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! simpleserver@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the simpleserver@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists,but node_modules missing,did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\User\AppData\Roaming\npm-cache\_logs\2020-10-19T13_55_19_012Z-debug.log

我是NodeJS的新手,我从youtube教程中学习。我也创建了index.html页面。我试图在以下位置运行index.html页面:“ http://127.0.0.1:3000/index.html”。 我确实做了他们所做的。我重新检查了任何错误。请帮助我...预先感谢

解决方法

process.cwd更改为process.cwd()。您正在使用旧版本的node.js中的语法。这是当前的API reference

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。