繁体   English   中英

Node JS fs创建新文件模块不在本地主机上运行

[英]Node JS fs create new file module not running on localhost

我正在尝试使用我的 Visual Studio ide 运行节点 js 代码,但索引文件未在本地服务器上运行,仅显示加载

var fs = require('fs');
var http=require('http');
var server=http.createServer(function(req,res)
{
    if (req=='/')
    {
        fs.writeFile('mynewfile3.txt', 'New text', function (error) {
           if(error)
           {
            res.writeHead(200,{'Content-Type':'text/html'});
            res.write('file was not written');
            res.end();
           }
           else{
            res.writeHead(200,{'Content-Type':'text/html'});
            res.write('file was written');
            res.end();
           }
          });
    }
});

server.listen(7070);
console.log('server run');

这是我的 index.js 文件

我还附上了 package.json 文件

{
  "name": "write",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",

      "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

谁能帮我解决这个问题?

当然:简短的回答: if (req=='/')永远是假的。

您可以将其更改为if (req.url === '/')之类的内容。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM