The server does not return any response when accessing the route

Asked

Viewed 161 times

0

[SOLUTION]

It was necessary for me to configure the Postman Authorization tab and select the Type Bearer Token option and pass the token

inserir a descrição da imagem aqui




When trying to access a route in my API using the POST method Postman said that the server did not send any reply.

Resposta do servidor

Then I decided to remove Header Authorization which contains my token and then he accessed the route and returned the response:

inserir a descrição da imagem aqui

Here’s a snippet of the server code where I import the routes:

app.use(cors());// <--- Pacote para habilitar CORS
app.use(express.static('public'));
app.use(bodyParser.json());


app.use('/auth', authRoutes);
app.use('/user', userRoutes);// <--- Ele segue por essa rota

And this is the middleware responsible for the route:

router.post('/posts', isAuth, (req, res, next) => {//O isAuth não interfere em nada. Eu já o removi e o comportamento continua o mesmo
    res.status(201).json({
        success: true,
        message: 'Foi'
    })
})

module.exports = router;

Why can’t I access this route using Header Authorization?

I’m using the Expressjs framework

  • I find it interesting to post what the isAuth doing it. By the description it doesn’t seem to be far from it.

  • Could you pass the type of post you’re doing? Depending on the type of post, your body pass might be set wrong. BP Docs: https://www.npmjs.com/package/body-parser Medium story: https://medium.com/@febatista107/how-to-convert-the-data of a-request-with-o-body-parser-2b5b93100f00

  • @Leocavalcante I purposely omit this so as not to create confusion because it would take the focus of the problem. I tested with and without isAuth and the result was the same.

  • Ryan Lucas I edited the post pointing out what the problem was

1 answer

0


The package cors that you’re using, talks in your description that more complex requests, such as requests with custom headers, may require a pre-flight OPTIONS request. Since you are not handling this request, your application should be ignoring the POST.

Try to put this line before your routes:

app.options('*', cors());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.