Reputation: 1400
I have a node.js server that replies to ajax request, I click on the webpage and an ajax call is sent and it runs the function but occasionally the server freezes, sometimes doing ctrl+c
unfreezes it and it continues processing the request, sometimes it doesn't and I have to restart it.
Code:
app.use(function(req, res, next) {
console.log("Header");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
console.log("Header sent!")
});
app.get('/', function (req, res) {
r.getSubreddit('HighRes').getRandomSubmission().then(function(post){
if(post.url.includes(".jpg")){
currentUrl = post.url;
console.log(currentUrl);
res.send(currentUrl);
res.end();
} else{
console.log("Wrong format");
}
});
console.log("Got request");
//res.end();
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
Upvotes: 0
Views: 646
Reputation: 9022
There is some issue with your /
route. In case, url contains jpg
, you are returning response, but, in other case, you aren't returning anything. Hence your request is in waiting state.
Try the following code.
app.get('/', function (req, res) {
r.getSubreddit('HighRes').getRandomSubmission().then(function(post){
if(post.url.includes(".jpg")){
currentUrl = post.url;
console.log(currentUrl);
res.send(currentUrl);
} else{
console.log("Wrong format");
res.send('Wrong format'); //you should return something here too
}
});
});
Upvotes: 1