Running node.js script in index.html file


#1

I am new to programming and was trying my hands on building a desktop app using electron.
With my app I am trying to upload a file from system to a server as the application is launched.
I successfully achieved the task as run the script independently in node.
However, when I sourced the script in index.html file of my desktop app it didn’t work.
Can anyone help me with exactly what went wrong!!
I used request package in my script while trying to make a post request to upload the file to the server.


#2

It certainly sounds like something that shouldn’t be an issue, but really we need more information (or, better yet, code snippets) to be able to help you with this.


#3

var request = require(‘request’);
var fs = require(“fs”);

var source = fs.createReadStream(’./input.txt’);

var req = request.post(someUrl, function (err, resp, body) {
if (err) {
console.log(‘Error!’);
} else {
console.log('URL: ’ + body);
}
});
var form = req.form();
form.append(‘file’, source);

The above is the request file. Its working when I run “node filename.js” in command prompt but not when
called from index.html and running “npm start” to launch my app.


#4

In what way is it not working? Does it open a window? Is it failing silently?

If you open the DevTools, what does it log when you change console.log('Error!'); to console.log(err);?


#5

It is opening the electron window and running the html part but the script is not working as in I am neither receiving the file on the server nor getting any acknowledgement of sending the file in my command prompt.
First I tried to source it in my index.html file but it wasn’t working. Instead of calling the file I even tried to write the script in the file(index.html) itself but that too wasn’t working(only the script part, the html part is working and I m able to see it in window.)


#6

Thank you very much caffiend for your interest in solving my problem. It was my bad. The script was running in the back. I got the hint from your opening DevTools suggestion. Well the problem is solved.