Answer the question
In order to leave comments, you need to log in
How to disable post and put in express.js?
How to disable POST and PUT requests in express.js? Express.js is connected to the MySQL DBMS. It is necessary to make sure that only users who are in the database can receive these requests.
Answer the question
In order to leave comments, you need to log in
Something like this:
const mysql = require('some-mysql-library');
const app = express();
// ...
function getUserFromMysqlMiddleware(req, res, next) {
mysql.query('SELECT * FROM `users` WHERE `id`=? LIMIT 1', [req.query.userid]).then(([user]) => {
if(!user) {
const err = new Error('Access denied');
err.status = 403;
return next(err);
}
req.user = user;
next();
}).catch(next);
}
app.post('/some/route', getUserFromMysqlMiddleware, (req, res) => {
// Ваш код с гарантированным наличием req.user
});
app.put('/some/route', getUserFromMysqlMiddleware, (req, res) => {
// Ваш код с гарантированным наличием req.user
});
if(user_has_rigth){
// Your code
}
else{
// you haven't paid for the premium plan, but you can pay for it on this page
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question