L
L
lordprotein2020-01-10 13:58:21
JavaScript
lordprotein, 2020-01-10 13:58:21

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

2 answer(s)
D
Dmitry Belyaev, 2020-01-10
@lordprotein

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
});

V
Vladimir Korotenko, 2020-01-10
@firedragon

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 question

Ask a Question

731 491 924 answers to any question