Glitch express.js app not receiving post request due to timeout of frontend"

I have build a an exprss.js server and placed in on glitch, but when I try to post a request to https://parrot-api.glitch.me it does not receive the post request, get requests do arrive.

Hi @TijsDeBelie, welcome to the Glitch forums! It looks like your project might be private – can you share a little more about the express configuration? Do you have an app.post call in it? (That was my problem when I had the same error!)

I tested it on localhost and it works perfectly fine there. Post are able to come from my frontend to my backend perfectly fine.

My code formatted : https://hastebin.com/hiqimemomi.js

my frontend code :

function PostRequest(data, path) {
console.log(postrequest ${path})
return new Promise((resolve, reject) => {
try {
request.post(domain + ‘:4000/’ + path, {
json: JSON.parse(data)
}, (error, res, body) => {
if (error) return reject(error)
console.log(statusCode: ${res.statusCode})
resolve(body)
})
} catch (error) {
reject(error)
}
});
}

my backend code :

app.post("/settings/new", function (req, res) {
Authenticate(req.body.key).then(e => {
if (!req.body.guild_id || !req.body.obj) {
console.log("Received incomplete POST: " + JSON.stringify(req.body));
return res.send({ “status”: “error”, “message”: “missing parameter(s)” });
} else {
console.log("Received POST: " + JSON.stringify(req.body));
LoadSettings()
.then(settings => {
settings[req.body.guild_id] = req.body.obj
WriteToSettings(settings)
.then(s => { res.send(s[req.body.guild_id]) })
.catch(e => {
return res.send({ “status”: “error”, “message”: Could not write to json file ${e} });
})
})
.catch(e => {
return res.send({ “status”: “error”, “message”: ${e} });
})
}
}).catch(e => {
return res.status(403).send(‘Not Authorized’)
})
});

I think the issue might be the port – try taking the :4000 out of the domain url? My understanding is that Glitch containers only leave certain ports open.

any clue what ports are open? since Express needs a value for a port to listen on

I removed the port and now the post is coming trough but does Glitch not allow code to write to files??

it does not adjust my json file with the data I posted

3000 is the default! Mine doesn’t even set it explicitly – it just grabs the process env port like this:

var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});
``

Hmmmmm what does the error say when it fails to write to the file (or is there no error?)

No error the file just doesn’t update. Tried the refresh command too.

Its not catching the error from LoadSettings().

Probably LoadSettings() and WriteSettings() have the same problem, not giving the right path for Glitch’s filesystem.

Try prepending __dirname to the filename to give it the module’s path.

“Choose from these three paths, and may the fourth be with you” :slight_smile:

1 Like