I'm creating an express project and I have some routes to provide data to frontend controllers via ajax, for instance they begin with /get_data
.
So my question is how to protect these routes? Anyone can access them easily. I tried to do
app.use((req, res, next) => {
if(!req.xhr) res.sendStatus(404);
else next();
})
But this doesn't prevent ajax calls from other sites to access the data. So how to make it more secure? If it's not possible, in what way can I provide data to frontend?