Cannot write on json file in express app nodejs - node.js

I'm trying to read and write some data in express application (in a json file which already exists and has some data) using FS nodejs module but it returns this error in console:
Object { errno: -13, code: "EACCES", syscall: "open", path: "data.json" }
It works in localhost, but not on my web server and I don't understand where is the problem.
I've tried several ways to write on file and I'm sure this one works perfectly.
SERVER SIDE CODE:
app.post('/addLocation', function(req, res){
var jsonFile = JSON.parse(fileSystem.readFileSync(path.join(__dirname,'data.json'),'utf8'));
jsonFile.locations.push(req.body); //json data is an array named 'locations'
fileSystem.writeFile('data.json', JSON.stringify(jsonFile, null, 2), function(err){
if (err) res.send(err);
else res.send('SERVER MESSAGE: data saved');
});
})
CLIENT SIDE CODE:
const data = {name, address, lat, lng, content, audience, detail, language, purpose, description};
$.ajax({
url: '/addLocation',
type: "POST",
data: data,
success: function(result){
console.log(result);
}
});
ERROR in console:
Object { errno: -13, code: "EACCES", syscall: "open", path: "data.json" }

Related

fs.writeFile not saving even when path is correct

Having an issue with fs.writeFile in my nodejs app running locally where im getting an error of this below,
I am running on localhost xampp also on windows if that may be an issue?
[Error: ENOENT: no such file or directory, open 'C:\Users\exampleuser\Desktop\examplenodejspath\product\sku123.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\exampleuser\\Desktop\\examplenodejspath\\product\\sku123.json'
}
Below is a copy of the code.
var product = {"SKU": "sku123","name": "test"};
fs.writeFile(__dirname + "/product/" + product.SKU + ".json", product, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("product was saved!");
});
I can confirm the path is correct. but for some reason it still returns its not correct.
Any help would be appericated

Uploading files error with express-fileUpload

I have an error uploading a file on the server if I can help
This is a command to get the file from the client and save it to the uploads folder
exports.adminSendFile = (req, res) => {
if (req.files === null) {
return res.status(400).json({ msg: 'No file uploaded' });
}
const file = req.files.file;
file.mv(`${__dirname}/SERVER/Servises/uploads/${file.name}`, err => {
console.log("****HERE*****");
if (err) {
console.error(err);
return res.status(500).send(err)
}
res.json({
fileName: file.name,
filePath: `/SERVER/Servises/uploads/${file.name}`
})
console.log(req.body),
vacations.create({
description: req.body.description,
destination: req.body.destination,
createdAt: req.body.createdAt,
updatedAt: req.body.updatedAt,
img: req.body.filePath,
price: req.body.price,
})
})
Imge>>>Clike-Me
You can see the Error here
{ [Error: ENOENT: no such file or directory, open 'C:\Users\Itzik\Desktop\לימודים\projact3\server\Servises\controller\SERVER\Servises\uploads\My Passport Photo.jpg']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path:
'C:\\Users\\Itzik\\Desktop\\לימודים\\projact3\\server\\Servises\\controller\\SERVER\\Servises\\uploads\\My Passport Photo.jpg' }
POST /users/admin/upload 500 40.394 ms - 189
__dirname is the most common method. It gives you the path of the currently running file.
As per your error , I think what path you are trying to get is this :
C:\Users\Itzik\Desktop\לימודים\projact3\server\Servises\uploads\My Passport Photo.jpg'
And not :
C:\Users\Itzik\Desktop\לימודים\projact3\server\Servises\controller\SERVER\Servises\uploads\My Passport Photo.jpg'
You need to go one step up from current directory with ../ and then you are good to go, like this :
file.mv(`${__dirname}/../uploads/${file.name}`

twilio-node unable to reach host "api.twilio.com" TLSWrap.onread

I am getting the following error when using the twilio node module
{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
{ status: 'ECONNRESET',
message: 'Unable to reach host: "api.twilio.com"' }
I am able to send SMS messages through Postman and their console interface but when trying it through NodeJS I get the error.
The code that I have is this:
var express = require('express');
var app = express();
var twilio = require('twilio');
var accountSid = 'ACXXXXXXXXXXXXXXX';
var apiKey = 'XXXXXXXXXXXXXXXXXXX';
var apiSecret = 'XXXXXXXXXXXXXX';
var twilioClient = new twilio.RestClient(accountSid, apiSecret);
//var account = twilioClient.accounts(accountSid);
//Send an text message
twilioClient.messages.create({
body: 'Hello from Node',
to: '+my_num', // Text this number
from: '+twilio_num' // From a valid Twilio number
}, function(err, message) {
if (err) {
console.log(err);
}
else {
console.log(message);
}
});
I found a similar problem here and followed the posted solution but that didn't work. I am guessing this is an entirely different error.

Error downloading file from my Node.js server

I'm trying to make a download link on my server for a zip file and I'm currently getting this error: (Note, still just testing it on local machine)
{ [Error: ENOENT: no such file or directory, stat 'C:\Users\Jordan\Desktop\Websites\HappyCamel\Users\Jordan\Desktop\Websites\HappyCamel']
errno: -4058,
code: 'ENOENT',
syscall: 'stat',
path: 'C:\\Users\\Jordan\\Desktop\\Websites\\HappyCamel\\Users\\Jordan\\Desktop\\Websites\\HappyCamel',
expose: false,
statusCode: 404,
status: 404 }
The relevant code is this:
router.get('/file/:name', function(req, res, next) {
console.log('test123'); //successfully prints to console
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
console.log('test456'); //successfully prints to console
if(err) {
console.log(err) //I'm assuming this is source of logged error
} else {
console.log("no error"); //doesn't print
}
});
})
edit:
Fixed it with changing this line:
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
to
res.download('./test123.zip', 'test123.zip', function(err) {
but now I get
angular.min.js:114 ReferenceError: success is not defined
error on my browser, but no errors in my node console (my "no error" line is printing)
you are using relative path. when you do this:
res.download('Users/Jordan/Desktop/Websites/HappyCamel/', 'test123.zip', function(err) {
it will look for Users/Jordan/Desktop/Websites/HappyCamel/ inside your current file's directory. looks like what you need is full path, or better a correct relative path- from the error it looks like the file is located with your code, so this should do:
res.download('./', 'test123.zip', function(err) {

Unable to upload .torrent with node.js and utorrent-api

I'm trying to use utorrent-api library for node.js as in example:
request({'uri' : 'http://releases.ubuntu.com/13.04/ubuntu-13.04-desktop-i386.iso.torrent', 'encoding': null}, function (error, response, torrentFileBuffer) {
utorrent.call('add-file', {'torrent_file': torrentFileBuffer}, function(err, data) {
if(err) { console.log('error : '); console.log(err); return; }
console.log('Successfully added torrent file !');
console.log(data);
});
});
and I'm getting this error in console:
error :
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
I can connect to uTorrent with:
utorrent.call('list', function(err, torrents_list) {
console.log(torrents_list);
});
And I'm getting torrents list correctly.
I tried to save .torrent file to disk and it looks ok, so the problem is with file upload.
uTorrent 3.3 is running on Linux. WebUI is working and I can upload .torrent files through browser.
How can I debug this error?

Resources