2 years ago
#18029
Osajay
External API calls not working in shared hosting but working perfectly on local host
I have a node js application that uses external APIs from a different backend. I made some requests using vanilla js and some from within my node js/express. They all work fine in my localhost until I deployed to production on cpanel.
Here is my code:
//ROUTE
router.route('/job-search')
.get(Controller.getSearch);
//CONTROLLER
const Request = require('request');
getSearch: (req, res) => {
if (req.query.position || req.query.location || req.query.type || req.query.level || req.query.sector) {
const position = req.query.position
const form = {
available_post: req.query.position,
sector_id: req.query.sector,
city_assignment_id: req.query.location,
job_type_id: req.query.type,
function_id: req.query.level
};
const type = req.query.type !== undefined ? Number(req.query.type) : '';
const level = req.query.level !== undefined ? Number(req.query.level) : '';
const sector = req.query.sector !== undefined ? Number(req.query.sector) : '';
const location = req.query.location !== undefined ? Number(req.query.location) : '';
const params = 'position='+req.query.position+'§or='+sector+'&location='+location+'&type='+type+'&level='+level;
//console.log("params: " + params)
const page = req.query.page ? Number(req.query.page) : 1;
Request.post({
"headers": {
"Content-Type": "application/json",
"Accept": "application/json",
},
"url": `https://jobatrac.com/api/job_postings/search?page=${page}`,
"body": JSON.stringify(form)
}, (error, response, body) => {
if(error) {
return console.log(error);
}
var results = JSON.parse(body)
const newIvory = results.data
const num_next = results.links.next
const num_prev = results.links.prev
const count = results.meta.total;
const fromValue = results.meta.from;
const toValue = results.meta.to;
if (num_next !== null && num_prev !== null ){
var previous = num_prev.replace(/[^0-9]/g,'');
var next = num_next.replace(/[^0-9]/g,'');
connection.query(`SELECT * FROM jobs WHERE (jobTitle LIKE '%${position}%') ORDER BY posted DESC`, (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, newIvory, fromValue, toValue, next, previous, params, count})
}else{
throw err;
}
})
}
else if (num_next !== null && num_prev == null ){
var next = num_next.replace(/[^0-9]/g,'');
connection.query(`SELECT * FROM jobs WHERE (jobTitle LIKE '%${position}%') ORDER BY posted DESC`, (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, newIvory, fromValue, toValue, next, params, count})
}else{
throw err;
}
})
}
else if (num_prev !== null && num_next == null ){
var previous = num_prev.replace(/[^0-9]/g,'');
connection.query(`SELECT * FROM jobs WHERE (jobTitle LIKE '%${position}%') ORDER BY posted DESC`, (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, newIvory, fromValue, toValue, previous, params, count})
}else{
throw err;
}
})
}else{
connection.query(`SELECT * FROM jobs WHERE (jobTitle LIKE '%${position}%') ORDER BY posted DESC`, (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, newIvory, fromValue, toValue, count})
}else{
throw err;
}
});
}
});
}else{
const page = req.query.page ? Number(req.query.page) : 1;
Request.get({
"headers": {
"Content-Type": "application/json",
"Accept": "application/json",
},
"url": `https://jobatrac.com/api/job_postings?page=${page}`,
}, (error, response, body) => {
if(error) {
return console.log(error);
}
var results = JSON.parse(body)
const newIvory = results.data
const num_next = results.links.next
const num_prev = results.links.prev
const count = results.meta.total;
const fromValue = results.meta.from;
const toValue = results.meta.to;
const allSearch = 1;
if (num_next !== null && num_prev !== null ){
var previous = num_prev.replace(/[^0-9]/g,'');
var next = num_next.replace(/[^0-9]/g,'');
connection.query('SELECT * FROM jobs ORDER BY posted DESC', (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, fromValue, toValue, newIvory, next, previous, allSearch, count})
}else{
throw err;
}
})
}
else if (num_next !== null && num_prev == null ){
var next = num_next.replace(/[^0-9]/g,'');
connection.query('SELECT * FROM jobs ORDER BY posted DESC', (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, newIvory, fromValue, toValue, next, allSearch, count})
}else{
throw err;
}
})
}
else if (num_prev !== null && num_next == null ){
var previous = num_prev.replace(/[^0-9]/g,'');
connection.query('SELECT * FROM jobs ORDER BY posted DESC', (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, newIvory, fromValue, toValue, previous, allSearch, count})
}else{
throw err;
}
})
}else{
connection.query('SELECT * FROM jobs ORDER BY posted DESC', (err, jobs) => {
if (!err){
res.render('default/job-search', {jobs: jobs, fromValue, toValue, newIvory, allSearch, count})
}else{
throw err;
}
});
}
}
)};
},
The requests I made using vanilla js are working fine but the requests using node packages aren't working. The server responds with "incomplete response from server". I have tried axios, and request packages but while they both work on local host, neither works on the server. I would try gotStream and node-fetch but my server has a node -12.22.7.
I have tried everything I can think of.
node.js
api
axios
request
shared-hosting
0 Answers
Your Answer