在服务器上运行节点js时遇到问题

时间:2018-02-17 18:41:21

标签: node.js api ubuntu server

我已经实现了一个node.js脚本,该脚本使用多个API从网站下载图像,将其保存到我的硬盘,然后将该图像上传到Instagram帐户

api是:

unsplash Instagram的和私营的API 图像下载

我已经通过命令行在我的本地Windows机器上成功执行了java脚本。

我目前正在运行Ubuntu 16.04服务器。我尝试过大量的在线教程,但似乎没有一个适合我。

当我在服务器上获取javascript文件,并尝试通过命令行运行它时,我收到错误

root@insta-bot:~/instagram/instagrambot/VintageCars/JS Scripts# nodejs upld_Cars                                                                                        .js
/root/instagram/instagrambot/VintageCars/JS Scripts/upld_Cars.js:8
  let data = '';
  ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet sup                                                                                        ported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3

这是我的javascript代码:

//Download
const https = require('https');
const download = require('image-downloader')

 var search ='sexy-car'
https.get('NOTREALSEARCHCRITERIA, (resp) => {
  let data = '';

  // A chunk of data has been recieved.
  resp.on('data', (chunk) => {
    data += chunk;
  });

  // The whole response has been received. Print out the result.

  resp.on('end', () => {
    photoUrl = JSON.parse(data).urls.custom;
    console.log(photoUrl);
    // Download to a directory and save with the original filename
const options = {
  url: photoUrl,
  dest: 'C:/Users/USER/Desktop/instagrambot/VintageCars/unsplash_Vintage_Car_dwnld/image.jpg' // Save to /path/to/dest/image.jpg
}

download.image(options)
  .then(({ filename, image }) => {
    console.log('File saved to', filename)
  }).catch((err) => {
    throw err
  })
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});


// Upload
var Client = require('instagram-private-api').V1;
var device = new Client.Device('FAKEDEVICE');
var storage = new Client.CookieFileStorage(__dirname + '/cookies/carVintagePage.json');

var username = 'NOTREALUSERNAME';
var pass = 'NOTREALPASS'

var Approved_Hashtags = ['#Beautiful ', '#Vintage ', '#Car ', '#Carstagram ', '#OnlyAutomobiles ',
                         '#OldStyle ' , '#Classic ', '#OldCars ', '#OldAutos ', '#ClassicCars ', 
                         '#CarPorn ', '#VintageCar ', '#Stunning ', '#Amazing '];

var caption = getCaption(Approved_Hashtags);

var pathOrStream = 'C:/Users/USER/Desktop/instagrambot/VintageCars/unsplash_Vintage_Car_dwnld/image.jpg';

Client.Session.create(device, storage, username, pass)
.then(function(session){
  return [Client.Upload.photo(session, pathOrStream), session]
})
.spread(function(upload, session) {
  return Client.Media.configurePhoto(session, upload.params.uploadId, caption)
})
.then(function(medium) {
  console.log(medium)
})

//////////////////////function////////////////////////////////

function getCaption(arr) {
    n = Math.floor(Math.random() * 7) + 3  
    var result = new Array(n),
        len = arr.length,
        taken = new Array(len);
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len;
    }

    var caption = '';
    for(var tag of result)
        caption+= tag;

    return caption;
}

我已经让脚本在本地完美地工作了。但在服务器上我收到上述错误。我已经安装了节点,所有API都使用了npm install。

我不确定为什么会收到此错误。任何指导将不胜感激

1 个答案:

答案 0 :(得分:0)

您需要更新nodeJS。 使用典型指令升级节点,如果不起作用:

sudo npm install -g npm
sudo npm install -g n
sudo n stable

运行which node

,找出您的节点所在的位置

sudo ln -sf / usr / local / n / versions / node / <VERSION> / bin / node / usr / bin / node

使用最高版本代替&#34; <VERSION>&#34;来自/ usr / local / n / versions / node

中的安装选项
相关问题