因此plesk / node出了点问题,因为以下代码在本地运行良好。我收到504网关超时错误。
full script for cronjob.js
const express = require('express');
const router = new express.Router();
var parser = require('fast-xml-parser');
var he = require('he');
const fetch = require('node-fetch');
router.get('/fuelprice/:type', async (req, res) => {
var fuelurl;
var ftype;
console.log(req.params.type);
function appendLeadingZeroes(n){
if(n <= 9){
return "0" + n;
}
return n
}
function objectFactory(prop) {
return {
[prop]: {
valid: false
}
};
}
const current = new Date();
var today = new Date();
var time = appendLeadingZeroes(today.getHours()) + ":" + appendLeadingZeroes(today.getMinutes()) + ":" + appendLeadingZeroes(today.getSeconds());
try{
if(req.params.type == 1) {
ftype = "91";
if(time > "14:29"){
console.log(time);
fuelurl ="https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98&Day=tomorrow";
}
else{
fuelurl = "https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98";
}
}
if(req.params.type == 2){
ftype = "95";
if(time > "14:29"){
console.log(time);
fuelurl ="https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98&Day=tomorrow";
}
else{
fuelurl = "https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98";
}
}
if(req.params.type == 4){
ftype = "Diesel";
if(time > "14:29"){
console.log(time);
fuelurl ="https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98&Day=tomorrow";
}
else{
fuelurl = "https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98";
}
}
else if(req.params.type == 5){
ftype = "LPG";
if(time > "14:29"){
console.log(time);
fuelurl ="https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98&Day=tomorrow";
}
else{
fuelurl = "https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98";
}
}
else if(req.params.type == 6){
ftype = "98";
if(time > "14:29"){
console.log(time);
fuelurl ="https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98&Day=tomorrow";
}
else{
fuelurl = "https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98";
}
}
else if(req.params.type == 10){
ftype = "E85";
if(time > "14:29"){
console.log(time);
fuelurl ="https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98&Day=tomorrow";
}
else{
fuelurl = "https://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS?Product="+req.params.type+"&StateRegion=98";
}
}
else{
}
const response = await fetch(fuelurl, function(error, meta, body){
// console.log(body.toString());
const data = body.toString();
var xmlData = data;
var options = {
attributeNamePrefix : "@_",
attrNodeName: "attr", //default is 'false'
textNodeName : "#text",
ignoreAttributes : true,
ignoreNameSpace : false,
allowBooleanAttributes : false,
parseNodeValue : true,
parseAttributeValue : false,
trimValues: true,
cdataTagName: "__cdata", //default is 'false'
cdataPositionChar: "\\c",
parseTrueNumberOnly: false,
arrayMode: false, //"strict"
attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a
tagValueProcessor : (val, tagName) => he.decode(val), //default is a=>a
stopNodes: ["parse-me-as-string"]
};
if( parser.validate(xmlData) === true) { //optional (it'll return an object in case it's not valid)
var jsonObj = parser.parse(xmlData,options);
}
// Intermediate obj
var tObj = parser.getTraversalObj(xmlData,options);
var jsonObj = parser.convertToJson(tObj,options);
var newdata = new Array();
var i = 0;
const mFuel = require('mongoose').model('fuelwatch');
for(var attributename in jsonObj['rss']['channel']['item']){
var t = jsonObj['rss']['channel']['item'][attributename]['trading-name'];
var p = (jsonObj['rss']['channel']['item'][attributename]['price'] / 100).toFixed(2);
var d = jsonObj['rss']['channel']['item'][attributename]['date'];
var lat = jsonObj['rss']['channel']['item'][attributename]['latitude'];
var long = jsonObj['rss']['channel']['item'][attributename]['longitude']
let setKey = `getdata.${d}`;
//let arrayFiltersKey = `elem.${d}`;
//newdata[i] = {"title":i};
newdata[i] = {'store': t, 'location': [{coordinates:[long,lat],type:"Point"}],'getdata':[{'date' : d, prices: {'91':{'price' : p}}}]};
// console.log({'getdata':[{'date' : d, $ftype : p }]});
// console.log({ 'getdata':{[d]:{'date' : d, [ftype] : p }} });
mFuel.findOneAndUpdate(
{'typeOfData':"fuelwatch",'store': t, 'location': [{coordinates:[long,lat], type:"Point"}]},
{
$addToSet: {
[setKey]: {[ftype]: p}
}
},
{
upsert: true
}, function(err) {
if (err) {
console.log('ERROR when submitting round');
console.log(err);
}
});
i++;
/* console.log("title"+": "+jsonObj['rss']['channel']['item'][attributename]['trading-name']);
var dollars = jsonObj['rss']['channel']['item'][attributename]['price'] / 100;
dollars.toLocaleString("en-US", {style:"currency", currency:"AUD"});
console.log("Price"+": $"+ dollars.toFixed(2));
console.log("Date: " + jsonObj['rss']['channel']['item'][attributename]['date']);
console.log("lat:"+jsonObj['rss']['channel']['item'][attributename]['latitude'] + " + Long :"+jsonObj['rss']['channel']['item'][attributename]['longitude']);*/
}
var datas = newdata;
/* const mFuel = require('mongoose').model('fuelwatch');
mFuel.insertMany(datas, function(err,r) {
if(err){console.log(err)}
});*/
res.send(datas);
});
} catch (error) {
res.status(404).json({ "errors":"No Access" });
}
});
module.exports = router;