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ā