为什么我的 http.createServer 不能在 NodeJS 中工作?

发布于 2025-01-10 06:06:02 字数 1523 浏览 1 评论 0原文

我的 NodeJS 代码需要帮助。该程序正在运行,但我的服务器尚未创建。有什么想法吗?问题是,我怀疑我的 fs.readFile 和 fs.writeFile 正在生成错误,但这没有意义,因为它会记录它们。另外,我的 http.createServer 语法是否有问题,例如 res.writeHead 的问题?

我的代码在这里:


let http = require("http");
let fs = require('fs');
let readline = require("readline");
let rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("Do you have an html file here? y/n ", function(bool) {
  if (bool == "y") {
    rl.question("What is its name? ", function(file) {
      fs.readFile(file, (err, data) => {
        if (err) console.log(err);
        rl.question("What code to add? ", function(additionalData) {
          
          http.createServer((req, res) => {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(data + additionalData);
            res.end();
          })
          fs.writeFile(file, data + additionalData, err => {
            if (err) console.log(err);
          })
        })
      })
    })
  }
  else {
    rl.question("New file name: ", function(name) {
      rl.question("Data: ", function(data) {
        fs.writeFile(name, data, err => {
          if (err) console.log(err);
        })
        fs.readFile(name, (err, data) => {
          if (err) console.log(err);
          http.createServer(function(req, res) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(data);
            res.end();
          })
        })
      })
    })
  }
})

I need help with my NodeJS code. The program is working but my server is not being created. Any ideas why? The problem is, I suspect my fs.readFile and fs.writeFile are generating errors, but that doesn't make sense because it would log them. Also, is there something wrong with my http.createServer syntax, like a problem with the res.writeHead?

My code is here:


let http = require("http");
let fs = require('fs');
let readline = require("readline");
let rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("Do you have an html file here? y/n ", function(bool) {
  if (bool == "y") {
    rl.question("What is its name? ", function(file) {
      fs.readFile(file, (err, data) => {
        if (err) console.log(err);
        rl.question("What code to add? ", function(additionalData) {
          
          http.createServer((req, res) => {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(data + additionalData);
            res.end();
          })
          fs.writeFile(file, data + additionalData, err => {
            if (err) console.log(err);
          })
        })
      })
    })
  }
  else {
    rl.question("New file name: ", function(name) {
      rl.question("Data: ", function(data) {
        fs.writeFile(name, data, err => {
          if (err) console.log(err);
        })
        fs.readFile(name, (err, data) => {
          if (err) console.log(err);
          http.createServer(function(req, res) {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.write(data);
            res.end();
          })
        })
      })
    })
  }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文