0

Below is my app.js code.

import bodyParser from 'body-parser';
import cors from 'cors';
import requestIp from 'request-ip';
import os from 'os';
import { AppRoutes, AuthRoutes } from './routes';


const app = express();
app.use(cors());
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next)=> {
  const clientIp = requestIp.getClientIp(req);
  logger.debug(JSON.stringify(req.socket.address()));
  logger.debug(`incoming IP ${clientIp}`);
  next();
});
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
     next();
});

// Api Routes.
app.use('/api/login', AppRoutes);
app.use('/api', verifyToken, AuthRoutes);


export default app;

Below is my index.js code. Below code is working fine for GET and POST but its not working for PUT. Its giving an error.

You don't have permission to access /api/save-user-profile/{user-name}.

import {
  getCustomersbyId
} from './controller/customer-controller';
import { Login } from './controller/login';
import {
  modelEdit,
  saveProfile
} from './controller/extension';

const AuthRoutes = Router();
const AppRoutes = Router();

AuthRoutes.get('/customers/', getCustomersbyId);
AuthRoutes.post('/model-entity-links/info', modelEdit);
AuthRoutes.put('/save-user-profile/:username', saveProfile);
AppRoutes.post('/', Login);

export { AuthRoutes, AppRoutes };

When I am changing PUT to POST it is working fine. But I want to do it using PUT. Am I missing here anything to configure PUT. I have tried below code as well in app.js. But still getting same issue.

       res.header("Access-Control-Allow-Origin", "*");
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
       res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

My saveProile code is as below.

export const saveUserProfile = (req, res) => {
  logger.debug({ method: 'saveUserProfile', message: MESSAGE.ENTER });
  const { username } = req.params;
  const userProfileInfo = JSON.parse(JSON.stringify(req.body));
  if (username &&  !isEmptyObject(userProfileInfo)) {
    postUserProfile(username, userProfileInfo)
    .then(r=>{
      res.status(200).send(r.data);
    })
    .catch(e=>{
      logger.error({ method: 'saveUserProfile', message: e.response ? JSON.stringify(e.response.data.response_message) : e });
      parseError(e, res);
    });
  } else {
    logger.error({ method: 'saveUserProfile', message: MESSAGE.ERROR_INVALID_PARAM });
    parseError(MESSAGE.ERROR_INVALID_PARAM, res, true);
  }
};


Nilesh
  • 518
  • 1
  • 9
  • 26

1 Answers1

0

I have resolved this issue. It was an apache server issue where PUT and DELETE operation was restricted. We have made changes in apache configuration and it worked. Thank you all for responses.

Nilesh
  • 518
  • 1
  • 9
  • 26