为什么我的 http.createServer 不能在 NodeJS 中工作?
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论