I'm deploying a node.js based application to IBM's Bluemix and have added a few features to one of the samples they provide. I've added an additional javascript file that makes an ajax call to PHP, but the PHP file is coming back as not found because my path is incorrect. I've tried putting the file everywhere and it's just not being found. I'm thinking (as a total node noobie) that I'm missing some mysterious configuration or something.
In the main directory (among other things), the structure is like this:
-- views
- index.ejs (this is the main displayed code)
-- public
- js
- custom.js (my added file)
- all the other necessary js files
- css
- img
- php - I added this directory
- get-twitter.php - I added this...custom.js makes an ajax call here
In custom.js, I have this:
$("#get-twitter").click(function(event) {
handle = $('#twitter-handle').val();
$.ajax({
url: 'php/get-twitter.php',
type: 'POST',
dataType: 'JSON',
data: {
handle: handle,
},
success: function(data) {
console.log(data);
$.each(data, function(index, val) {
console.log(val.text);
});
}
});
});
When I try to make this call, the file isn't found, but the path is this: https://myapp.mybluemix.net/php/get-twitter.php It should be in views/php/get-twitter.php, but I'm guessing this is a configuration issue on my end.
I've tried every iteration of this: url: 'php/get-twitter.php', and put the PHP file in every directory and nothing is working.
What am I missing here?
There is no way to run PHP scripts from node. You will need to set up a server that supports PHP, and make your requests there. One option would be Apache.
Realistically, you probably don't want to set up an entire server for the purpose of running a single PHP script. A more reasonable solution would be to port the PHP script to run on Node. There are many packages for twitter API integration on NPM (e.g. the twitter module).
If the server has PHP installed, then you can try to use exec to run the PHP file using the command line interpreter and get the output. Obviously you need to refactor your code to work with arguments passed by shell ($args) and display a JSON response that will be catched by node. For example:
exec('php -f /project/file.php', function (err, out) {
if (!err) {
const output = JSON.parse(out);
// do something with the object
}
});
As #positlabs said, keep the things simpler and port the script to JavaScript.
Related
I am currently adding new features to a rather big project which has been running for a couple of years. The project uses a Couch DB database, Hapijs in the backend and React in the frontend.
My goal is to add a download button that let's the user (admin) download all the .pdf files from the database sorted by year and bundled in a .zip archive. The server already has a route for similar tasks, which is why I'm expanding the functionality of this route instead of creating a new one from scratch.
The /generate/{query} route handles all the file exports depending on the params in the query string. The query params will always contain the year. The route starts like this:
server.route({
method: 'GET',
path: '/generate/{query*}',
handler: async function(request, h) {
...
}
})
This route starts a long series of asynchronous processes and in the end successfully stores all the .pdf files in a tmp folder, sorted by year and bundled in .zip files. The only thing left to do would be to send the .zip file as a response back to the frontend, where the download is triggered.
This is where I'm having problems:
I am using the inert package to add files to my response with the file() method. If I pass in the path as a hardcoded string (e.g. file('/tmp/2019.zip')), the data is sent to the frontend correctly and the download starts as it should. However, if I try to build the same string dynamically, I get a corrupted .zip file that's only 1MB - 3MB (the original file is 250MB) (once it was 80MB, once 150MB, but both files were corrupted and couldn't be unpacked). I have tried a couple of different approaches to get a dynamic file path (template literals etc.), but they all produced the same result. This is what I'm currently stuck with:
const response = h.file(Path.join(__dirname + '/tmp/' + request.query.year + '.zip'), {
mode: 'attachment',
})
return response
I then created the following test route:
server.route({
method: 'GET',
path: '/test/{query*}',
handler: function(req, res) {
return res.file(Path.join(__dirname + '/tmp/' + req.query.year + '.zip'), {
mode: 'attachment'
})
}
})
...and it works like a charm, no matter which year I pass in as a query param.
This makes it difficult to pinpoint the exact part of my code that causes trouble: there are no issues with dynamically generated file paths in my test route, so that can't be it. The code in the actual route is also executed correctly, but only if the path to the file is hardcoded, so it's obviously not completely broken. For testing purposes I am currently not removing files from the tmp folder, so it can't be a problem with my async functions (the differently sized corrupted .zip files made me wonder if this might have something to do with my problem). But... what else could it be? Why can't I pass dynamic file names in my main route?
Has anybody here ever encountered anything like this? Let me know if you need more details. Any help is appreciated, even the slightest hunch could be of help!
I don't know if this is a duplicate question but i have searched and couldn't found solution for this
I am newbie in cpanel and i recently uploaded my project in it. Now there is a part in my website where i am loading a folder of images through jquery ajax. Now this was working perfectly in the local server xampp but not in the server it keeps giving 404 error that means that the files not being discovered by the ajax script. For security reasons i am not going to share the links right now but i will explain the full procedure
These are the location of those folders. These scripts are in js folder. But obviously it is included in index page. anyway lets move
var svgFolder = "img/svg/";
var productImagesFolder = "img/ImagesForProducts/";
Following are the ajax scripts that i am using to load the images of these folders
$.ajax({
url: svgFolder,
success: function (data) {
$(data).find("a").attr("href", function (i, val) {
if (val.match(/\.(jpe?g|svg)$/)) {
$(".svg-shapesDiv").append("<img src='" + svgFolder + val + "' id='svg-shapes' loading='lazy'>");
}
});
}
});
$.ajax({
url: productImagesFolder,
success: function (data) {
$(data).find("a").attr("href", function (i, val) {
if (val.match(/\.(jpe?g|jpg)$/)) {
$("#avatarlist").append("<img style='cursor:pointer;' class='img-polaroid' src='" + productImagesFolder + val + "' loading='lazy'>");
}
});
}
});
All of this is working fine in localhost server but for some reason when i uploaded them in the cpanel it stopped working.
I tried hard coding the img tag like this
<img src='img/svg/file.svg' id='svg-shapes' loading='lazy'>
<img src='img/ImagesForProducts/file.png' id='svg-shapes' loading='lazy'>
Things i tried
And this works fine so i think that the ajax is not figuring out the address. I also tried to search the image through link in the browser like this domainname.com/img/svg/file.svg and it works fine as well. i also tried to give ajax the path like this domainname.com/img/svg/file.svg but it doesn't work. I checked the file capitalization etc but everything is correct
If this was a stupid question then i am sorry but i don't know that what i am doing wrong and i am also new to cpanel and live hosting stuff.
Based on the response to my comment it sounds as though your xampp has "indexes" enabled by default. Please see here: https://httpd.apache.org/docs/2.4/mod/mod_autoindex.html
It may be that on your shared webhosting they are disabled by default and you would need to enable them for those 2 directories. As you are using cpanel please see here: https://docs.cpanel.net/cpanel/advanced/indexes/82/ but this can also be achieve by adding a .htaccess file to the 2 folders containing Options +Indexes.
The trouble with relying on indexes this way is that different servers could potentially return slightly different html so you could find that your xampp server returns html links (your JavaScript searches for anchor tags and gets the href from there) but the shared server may not return links it may just return the file names. Also with this html being returned your JavaScript has to parse that html, search all links and extract the href. I would therefore recommend writing a php script that gathers the relevant files and returns only those in JSON format. Much easier then for the JavaScript to parse and use and you now have full control of what is returned whether it is on your xampp server or other hosting. You can call this script whatever you want and you can place it wherever you want. You could even have one script that accepts query parameters from your AJAX call and from those it know which folder to look into and what types of files it must gather from the folder. This also has the advantage of keeping all other files in those folders hidden from prying eyes.
So this may be a dumb problem but I've been trying to load a text file from a static directory by using some simple ajax and then insert it into an html page.
I'm not really sure why the thing's giving me a 404 when I make the request, I thought the static directory would allow this. I'd like to avoid having to make individual app.get() for every file in that folder.
I've tried various ways to declare the path to the static folder but nothing seems to work, I might just be missing something though.
Server side is Node with ExpressJS, client side is just typical javascript/jQuery. The text file is literally just two paragraphs of lorem ipsum.
Here's the relevant portions of code:
the ajax request for the document where I want to insert:
$(document).ready(function() {
$.ajax({
url: 'articles/latest.txt',
dataType: 'text',
method: 'GET',
success: function(data) {
$('#latestPost').append(data);
}
});
});
and the express static that I'm trying to serve.
app.use(express.static('/articles'));
EDIT (solution found):
I'm an idiot and I fixed it, using this fixed everything and now stuff loads fine. Thanks makozaki for the help anyway.
app.use('/articles', express.static(__dirname + '/articles', {index: 'false'}));
So I'm making a browser based game, and in order to create an account for this game I have the JS file call a PHP file (POST) to write an XML file.
This works, I get the file in cPanel, in the right directory, with the right content. Meaning I can open it, but only in cPanel. When I try to access it via browser I get a 404, but only for about 30 min, then it'll just magically start working.
This same PHP file is called later on in the game to update XML files, and the same thing happens. I can confirm that the PHP works exactly as it should, because I can see that the file/directory is perfect.
Here's the interesting bit, if I create an XML file manually or update it manually, it works instantly. It's only the XMLs created by the PHP file that take forever to load.
It's like the server doesn't realize that there was a change on it, until half an hour after the fact. That is, unless I did it manually.
My PHP:
<?php
$filename=$_POST['fileTo'];
$newfile=fopen($filename,"w")or die('Can not open');
$string=$_POST['stuff'];
fwrite($newfile,$string) or die('Could not write');
fclose($newfile);
?>
My AJAX call:
$.ajax({
type: 'GET',
url: writeDirect,
dataType: 'xml',
success: function(result) {
},
cache:false,
error: function(error) {
$.post('PHP/Accounts/creatAcc.php', { fileTo: userWrite, stuff: writeStuff }, function() {
signIn(userATFS, passCe);
});
}
});
Update:
I've decided to access the games directory directly from the browser. This gets even more interesting.
First thing I did was create a new account called testFile, I get the standard error on the GET because the game can't access the newly created account.
Then I opened the directory in Chrome, this is interesting:
The index clearly shows that testFile.xml exists
Then I try clicking on it, but this is where it breaks
The images 404 despite the file clearly existing
And no, changing the permissions on testFile.xml did not change anything.
I believe I've found the answer. I think it was just that server that was weird like that. I was using x10 basic and decided to switch over to another service and now it works.
I have a very simple Python file, called python1.py, whose contents are:
f = open('C:\\Temp\\test.txt', 'w')
f.write('Succeeded')
f.close()
I wish to execute this from JavaScript, like so:
jQuery.ajax({
type: "POST",
url: "/cgi-bin/python1.py",
success: function (msg) {
alert("Data Saved: " + msg);
}
});
However, all that happens is that I get an alert showing me the contents of the Python script. The file C:\Temp\test.txt does not get created, so clearly the Python was not executed.
How do I persuade the code to execute the Python script instead of just reading it?
You simply need to configure your web server to execute your *.py scripts, instead of serving them as plain text.
If you are using Apache as a web server, you need to enable mod_python or mod_wsgi.
EDIT:
Since you are using using Apache, you may want to check the following article, which briefly describes how to set up the mod_python module:
A Brief Introduction to Apache's mod_python Module
You could also use the opensource project Pico. It's a really elegant way of calling server side Python code from client side Javascript.
The author has provided some simple examples here https://github.com/fergalwalsh/pico/wiki/Example-1:-Hello-World
Are you able to execute the script directly from the browser. This looks more like a webserver config issue than jquery's
If your script is that simple, you would be best off using CGI on the server side rather than mod_python or mod_wsgi as suggested by others. For details on how to set up Apache for CGI with Python and simple script examples see:
http://webpython.codepoint.net/cgi_tutorial