我的代码:
var prompt = require('sync-prompt').prompt;
var input = prompt('What is it you are after? ');
var mkdirp = require('mkdirp');
var fs = require('fs');
var url = require('url');
var http = require('http');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var unzip = require('unzip');
console.log('Okay, we will do our best to find ' + input + ', ' + ' ' + 'hold on.');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root', //just using root for my personal testing.
password: 'root',
database: 'Sonic'
});
connection.query('SELECT * FROM Sonic_URL WHERE Name="' + input + '"', function(err, rows, fields) {
if (err) throw err;
var Name = (fields[0].Uname);
if (Name === input) {
console.log("Found it! Just getting the download!");
}
}
//download progress bar
var ProgressBar = require('progress');
var bar = new ProgressBar(':bar', {
total: 10
});
var timer = setInterval(function() {
bar.tick();
if (bar.complete) {
console.log('\ncomplete\n');
clearInterval(timer);
}
}, 100);
//Building the download//
var spawn = require('child_process').spawn;
//Location of download.
var DOWNLOAD_DIR = './Sonic-Downloads';
//Making sure the Dir exists
var mkdir = 'mkdir ' + DOWNLOAD_DIR;
var child = exec(mkdir, function(err, stdout, stderr) {
// if (err) console.log('Awww snap! d') Ingnore this
download_file_httpget((rows[0].Address));
});
}
// Function to download file using HTTP.get
var download_file_httpget = function(sqlres) {
var options = {
host: url.parse(sqlres).host,
//port: ,
path: url.parse(sqlres).pathname
};
var sqlres = url.parse(sqlres).pathname;
var file = fs.createWriteStream(DOWNLOAD_DIR + sqlres);
http.get(options, function(res) {
res.on('data', function(data) {
file.write(data);
}).on('end', function() {
file.end();
console.log('Succsesfully downloaded ' +
namematch);
// Other things that need to be closed go here
process.exit();
});
});
};
});
//shut down code
process.on('SIGINT', function() {
console.log(
"\nGoodbye!a!"
);
// some other closing procedures go here
process.exit();
})
当我使用node命令运行此代码时,我得到了这个。
SyntaxError: missing ) after argument list
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
完成它所说的之后,我就明白了。
SyntaxError: Unexpected token var
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
不确定为什么这样做,我已经检查确保所有,{},[],()等匹配,关闭等等,任何帮助将不胜感激。
答案 0 :(得分:0)
if (Name === input) {
console.log("Found it! Just getting the download!");
}
}
删除了一个括号
connection.query('SELECT * FROM Sonic_URL WHERE Name="' + input + '"', function(err, rows, fields) {
if (err) throw err;
var Name = (fields[0].Uname);
if (Name === input) {
console.log("Found it! Just getting the download!");
}
//download progress bar
var ProgressBar = require('progress');
var bar = new ProgressBar(':bar', {
total: 10
});
var timer = setInterval(function() {
bar.tick();
if (bar.complete) {
console.log('\ncomplete\n');
clearInterval(timer);
}
}, 100);
//Building the download//
var spawn = require('child_process').spawn;
//Location of download.
var DOWNLOAD_DIR = './Sonic-Downloads';
//Making sure the Dir exists
var mkdir = 'mkdir ' + DOWNLOAD_DIR;
var child = exec(mkdir, function(err, stdout, stderr) {
// if (err) console.log('Awww snap! d') Ingnore this
download_file_httpget((rows[0].Address));
});
})