Script keeps falling. But why?

There is a code. It’s logic is at first to get file from a client (uploadFile), then load it to another server (changeImage), then load the answer (loadFile). It’s all done with promises to chain them together but it doesn’t work. It keeps to fall dawn. I have tried to change it a lot. Well actually I spent this whole day trying. But there is no result. In this version it’s falling without any mistakes in console. Can you help me?

var fs = require('fs'),
  http = require('http'),
  url = require('url'),
  multiparty = require('multiparty'),
  request = require('request');

var server = new http.Server();

var ifs = require('os').networkInterfaces();
var result = Object.keys(ifs)
  .map(x => [x, ifs[x].filter(x => === 'IPv4')[0]])
  .filter(x => x[1])
  .map(x => x[1].address)[2];
console.log('\nUse this ip: ' + result);
console.log("Successfully started\n");

server.listen('80', result);
server.on('request', onRequest);

function onRequest(req, res) {
  var parsed = url.parse(req.url, true);
  switch (parsed.pathname) {
    case '/':
    case '/index.html':
      fs.readFile('index.html', function(err, file) {
        if (err) res.end();
    case '/file':
        .then(function(a) {
          return changeImage({
              'uploadfile': fs.createReadStream('./files/temp.jpg'),
              'ef-set': 10,
              'ef-set-2': 10,
              'jpeg-quality': 80
            }, '',
            new RegExp(/download\.php\?file=.+?\.jpg/))
        .then(function(link) {
          //it falls before here because console.log('H') here won't show 'H' :-|
        .then(function() {
          return changeImage({
              'uploadfile': fs.createReadStream('./files/temp.jpg'),
              'efset1': 2,
              'outformat': 2,
              'jpegtype': 1,
              'jpegqual': 85,
              'jpegmeta': 1
            '', '',
            new RegExp(/https:\/\/.+?\.jpg/)
        .then(function(link) {
        .then(function() {
        .catch(function(err) {
          console.log('ERR ', err);
      fs.readFile('./' + req.url, function(err, file) {
        if (err) res.end();

function uploadFile(req) {
  if (fs.existsSync('./files/temp.jpg')) {
    fs.unlink('./files/temp.jpg', function(err) {
      if (err) reject(err);
  return new Promise(function(resolve, reject) {
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
      var path = files.uploadfile[0].path;

      fs.copyFile(path, './files/temp.jpg', function(err) {
        if (err) reject('ERRinCOPYING');
        fs.unlink(path, function(err) {
          if (err) reject(err);
          var a = 0;
          var timer = setInterval(function() {
            if (fs.existsSync('./files/temp.jpg')) {
          }, 10);

function changeImage(formData, url, link, regExp) {
  return new Promise(function(resolve, reject) {{
      url: url,
      formData: formData
    }, function(err, resp, body) {
      if (err) reject('ERRinREQUEST: ' + err);
      link += body.match(regExp);
      if (link.length > 32) {
      } else {
        reject('ERROR! LINK WAS NOT FOUND');

function loadFile(link) {
    .on('response', function(response) {

Well the assumption would be that your changeImage function is failing, and returning a rejected promise. instead of just rejecting in that function, try logging the error as well as sending the reject?

Console shows nothing

It shows NOTHING? So it doesnt even say “Use this ip” or any of the rest of it?

No, it shows it. It shows exactly it. and nothing more. For example if I uncomment the line with console.log('H") it won’t come to it.

It stops working in ‘return new Promise()’ in changeImage() as console.log put there shows nothing

What does the Network tab show? With promises the network tab can help you to find out if any of your requests are being rejected.

where can I find the Network tab?

Here’s some info on where to find the network tab.

The thing is if I comment the uploadFile() and run the rest of the script then everything works fine, and the opposite if I comment the rest of the script and leave only uploadFile() uncommented then again everything works.

That’s why it’s important to use the network tab to learn more about what is happening with your network requests,

  1. Here is a screenshot of the Network tab.
  2. Here is the code on the client side
document.forms.f.onsubmit = function(e) {
      var xhr = new XMLHttpRequest();'POST', '/file', true);
      var d = new FormData(this);

          xhr.onload = function(){,,,}

As I understand there a mistake occurs while uploading the file on the server though this file anyway appears on the server side

Click on the red file and you’ll get several tabs of information to learn about it, including the headers and response,

I just solved this problem separating the requests and making two of them… The first for saving the file and the second for changing Image
Everything works now

1 Like

The script started to fail again.
I changed loadFile function to return a promise because the file it’s going to load doesn’t make by the time it’s needed for another function.

Can you explain me what exaclty I should look for in the network tab? It fails in the second changeImage()

The response area is where you can find info from the server about why I failed, and the headers area gives you details about what caused that, with that which was sent to the server

Ive screenshoted the tab, but I don’t see any useful info here

Actually this tab I think won’t show anything because the server doesn’t send any response. It just silently fails and that causes the mistake on the client side

What does the preview and response tab show?

Somehow I ran the script with “node server” not as usuall with “supervisor server” and it started to work. But why?