'Why isn't my http.createServer working in NodeJS?
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();
})
})
})
})
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
