简体   繁体   English

第一个参数必须是字符串类型或缓冲区或 uint8array 的实例。 收到未定义

[英]First argument must be of type string or an instance of buffer or uint8array. Received undefined

I made this code and after everything I try it keeps giving me this error.我制作了这段代码,在我尝试了一切之后,它一直给我这个错误。

First argument must be of type string or an instance of buffer or uint8array. Received undefined

Excuse me for being new at Node.js, but I really don't know where I went wrong.对不起,我是 Node.js 的新手,但我真的不知道我哪里出错了。 I thank you for your help.我感谢你的帮助。

function createServer(obj){
  var i;
  obj.port = (obj.port || 8080);
  obj.path = (obj.path || "/");

  http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html'});
    for(i in obj.path){
      fs.readFile(i, "utf-8", (err, data) => {
        if(err){
          console.log(err);
        }
        res.write(data);
        res.end();
      })
    }
    
  }).listen(obj.port);

  return obj;
}

Your index.js file [https://github.com/notJudahRR/Firwe/blob/main/index.js] set path as an object:您的 index.js 文件 [https://github.com/notJudahRR/Firwe/blob/main/index.js] 将路径设置为 object:

const firwe = require("./src/index.js");

let server = firwe({
  port: 8080,
  path: {
    "/": "index.html"
  },
});
server.initServer();

Then, in server.js [https://github.com/notJudahRR/Firwe/blob/main/src/server.js] you must handle appropriately:然后,在 server.js [https://github.com/notJudahRR/Firwe/blob/main/src/server.js] 中,您必须正确处理:

const http = require("http");
const fs = require("fs");
const type = require("./type.js");

function createServer(obj) {
  var i;
  obj.port = obj.port || 8080;
  obj.path = obj.path || {
    "/": "index.html",
  };

  http
    .createServer((req, res) => {
      res.writeHead(200, { "Content-Type": "text/html" });
      Object.values(obj.path).forEach((v) => {
        fs.readFile(v, "utf-8", (err, data) => {
          if (err) {
            console.log(err);
          }
          res.write(data);
          res.end();
        });
      });
    })
    .listen(obj.port);

  return obj;
}

module.exports = createServer;

Object.values loop in each object property value and then you get the index.html to get the file to render. Object.values 在每个 object 属性值中循环,然后获取index.html以获取要渲染的文件。

暂无
暂无

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

相关问题 TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array \"path\" 参数必须是字符串类型或 Buffer 或 URL 的实例。 从 nodejs 收到 undefined - The \"path\" argument must be of type string or an instance of Buffer or URL. Received undefined from nodejs Nodejs:[ERR_INVALID_ARG_TYPE]:“data”参数必须是string类型或者Buffer的实例等收到Array的实例 - Nodejs: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, etc. Received an instance of Array TypeError:第一个参数必须是字符串、Buffer、ArrayBuffer、Array 或类似数组的 Object 类型之一。 在 cryptoJS 中收到类型 object - TypeError: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object in cryptoJS “chunk”参数必须是字符串类型或 Buffer 的实例 - The "chunk" argument must be of type string or an instance of Buffer ERR_INVALID_ARG_TYPE - “from” 参数必须是字符串类型。 接收到一个 Array 实例 - ERR_INVALID_ARG_TYPE - The “from” argument must be of type string. Received an instance of Array ytdl:“url”参数必须是字符串类型。 接收类型未定义 - ytdl: The "url" argument must be of type string. Received type undefined [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或缓冲区的实例。 使用 admin.auth().verifyIdToken 时 - [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. when using admin.auth().verifyIdToken “string”参数必须是 string、Buffer 或 ArrayBuffer 类型之一。 收货型 object - he "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type object TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是 string、Buffer、ArrayBuffer、Array 或类似数组的类型之一 Object - TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM