'Why node js is not creating new file again after deleting it
I wrote a Node js program with the following steps -
- Check if file ~/temp.txt exists
- If not create new file
- Write some content into the file
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
const homedir = os.homedir();
if (fs.existsSync(`${homedir}/temp.txt`)) {
} else {
fs.writeFile(`${homedir}/temp.txt`, '', { flag: 'wx' }, function (err) {});
}
// writing some content to the file
// ...
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
}
When I am running curl localhost:3000 -
- First, it did not find any file. So it created new file and write content as expected
- Now I deleted the file manually and hit the
curlcommand again - Now it identified file does not exist, but not creating new one.
I am unable to understand why it is creating the file successfully for the first time and not creating after deleting it manually.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
