Sign in
Log inSign up

Urgent! Help needed with Axios.

Deactivated User's photo
Deactivated User
·Apr 29, 2020

I really need to submit this tomorrow. Anybody please help!!!!!!!!!!!!!!!!!!!!!

Code works in Postman

Screenshot (223).png

Same code does not work in Axios

Annotation 2020-04-30 023416.png

AT THE EXPRESS API SERVER

index.js

require('dotenv').config();
const express      = require('express'),
      app             = express(),
      bodyParser = require('body-parser');

const loginRoute = require('./routes/loginRoute'),
   contentRoutes = require('./routes/contentRoutes');

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

const auth = require('./middleware/auth')

// For Cross-Origin Requests
app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});


app.use('/user/login', loginRoute);

app.use('/content/:number', auth, contentRoutes);

app.get('/het',function (req,res) {
    res.send(req.body.het);
})

var PORT = process.env.PORT || 7000;

app.listen(PORT, function(){
    console.log(`serving on ${PORT}`);
})

middleware/auth.js

const jwt = require('jsonwebtoken');

function auth(req,res,next) {
    console.log('body and params are ',req.body, req.params.number);
    const number=req.params.number;
    if (!('token' in req.body)) {
        return res.status(400).json({null:null});
    }
    jwt.verify(req.body.token, process.env.JWT_SECRET, (error,decoded)=>{
        if (error){
            // console.log(error)
            return res.status(404).json({null:null});
        }

        else{
            if (decoded.number!==number)
            {
                console.log(decoded.number, number);
                return res.status(403).json({flash:'Oops! Try logging in again'})
            }
            return next();
        }
    })
}

module.exports=auth;

routes/contentRoutes.js

const express = require('express'),
      router  = express.Router({mergeParams:true});

const messagesHandler = require('../handlers/messagesHandler'),
      mediaHandler      = require('../handlers/mediaHandler'),
      logsHandler      = require('../handlers/logsHandler');

const auth = require('../middleware/auth');

router.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

router.route('/messages')
    .post(auth,messagesHandler.findmessage)
    .delete(messagesHandler.deletemessage)

router.route('/media')
    .post(mediaHandler.findmedia)

router.route('/logs')
    .post(auth,logsHandler.findlogs)

module.exports = router;

If you don't understand the code or need more code, please reply

Please answer ASAP

The request with axios

AT THE CLIENT SERVER (SAY, localhost:500)

axios({
        method:'post',
        url: 'localhost:7000/content/whatsapp:1234/messa…,
        data: {token:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJudW1iZXIiOiJ3aGF0c2FwcDoxMjM0IiwiaWF0IjoxNTg4MTgyNTcyLCJleHAiOjE1ODgyNjg5NzJ9.I6FGy8GmKlRKc897o-E1UBx1NARos6YhCNW2XTLI1es'}
        })
        .then(response=>response.data)
        .catch(e=>e)

I will really be grateful to you

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct