const receiveFile = (filepath, req, res) => {
const wstream = fs.createWriteStream(filepath, {flags: 'wx'});
req
.pipe(wstream)
.on('error', (e) => {
if (e.code === 'EEXIST') {
res.statusCode = 409;
res.end('File Already Exists');
return;
}
res.statusCode = 500;
res.end('Internal Server Error');
})
.on('close', () => {
res.statusCode = 201;
res.end('File Created');
});
res.on('close', () => {
if (res.finished) return;
fs.unlink(filepath, (err) => {});
});
};
fs.readFile("./files_to_help/QuoteNum.txt", {encoding: "utf8"}, (err, data) => {
const intData = +data.toString();
console.log(intData);
// or make some other things to data...
});
Promise.resolve('Hello world').then(console.log).then(console.log);
function readFileAsync(path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, function (error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
(async function() {
try {
const data = await readFileAsync('./data.json');
} catch(error) {
console.log(error);
}
})();