jkyberneees / fastify-gateway

A Node.js API gateway that just works!
https://www.npmjs.com/package/k-fastify-gateway
MIT License
107 stars 14 forks source link

Cant send post body #42

Closed parthibd closed 5 years ago

parthibd commented 5 years ago

I am trying to send a post request by it returns an error saying "Unsupported Media Type" . What could I be doing wrong ?

jkyberneees commented 5 years ago

Hi @parthibd, would you please describe more details about the gateway configuration and the request you are making?

Thanks and Regards

parthibd commented 5 years ago
var jwt = require('jsonwebtoken');
require('dotenv').config();
const fastify = require('fastify')({
    logger: true
});

const jwtExceptionUrls = [
    `${process.env.ENDPOINT1_PREFIX}/api/login`,
    ".jpg",
    ".jpeg",
    ".bmp",
    ".gif",
    ".png"
];

function checkExceptionUrls(url) {
    for (let exceptionUrl of jwtExceptionUrls) {
        if (url.endsWith(exceptionUrl))
            return true;
    }
    return false;
}

const checkJwtTokenMiddleware = function (req, res, next) {

    if (checkExceptionUrls(req.url)) {
        next();
    } else {
        let token = null;

        if (req.headers.authorization == null) {
            res.setHeader('Content-Type', 'application/json');
            res.statusCode = 401;
            res.end(JSON.stringify({error: "Token missing in header.Please specify"}));
        } else if (req.headers.authorization.startsWith('Bearer ')) {
            token = req.headers.authorization.slice(7, req.headers.authorization.length).trim();
            try {
                jwt.verify(token, process.env.JWT_SECRET);
                next()
            } catch (err) {
                res.setHeader('Content-Type', 'application/json');
                res.statusCode = 401;
                res.end(JSON.stringify({error: err.message}));
            }
        } else {
            res.setHeader('Content-Type', 'application/json');
            res.statusCode = 401;
            res.end(JSON.stringify({error: "Token missing in header"}));
        }
    }
};

fastify.register(require('fastify-reply-from'));
fastify.register(require('k-fastify-gateway'), {
    middlewares: [
        require('cors')(),
        require('helmet')(),
        checkJwtTokenMiddleware
    ],
    routes: [
        {
            prefix: process.env.ENDPOINT2_PREFIX,
            target: process.env.ENDPOINT2,
            middlewares: []
        },
        {
            prefix: process.env.ENDPOINT3_PREFIX,
            target: process.env.ENDPOINT3,
            middlewares: [],
        },
        {
            prefix: process.env.ENDPOINT1_PREFIX,
            target: process.env.ENDPOINT1,
            middlewares: [],
        },
    ]
});

fastify.listen(process.env.GATEWAY_LISTEN_PORT, '0.0.0.0').then((address) => {
    console.log(`API Gateway listening on ${address}`)
});
parthibd commented 5 years ago

This is the code . Now say I am making a POST request to http://localhost:8090/endpoint1_prefix/api/photo as a multipart request , fastify returns that unidentified body type . What could I be doing wrong ?

jkyberneees commented 5 years ago

Hi @parthibd, I think this thread will be of your help: https://github.com/fastify/fastify/issues/202

Thanks and Regards

parthibd commented 5 years ago

I tried this solution, but it does not work.

jkyberneees commented 5 years ago

This issue is related to fastify, meaning is not being originated by this module.

I may recommend you to have a look at this other alternative I have created: https://www.npmjs.com/package/fast-gateway It is faster, and not bound to fastify. More details also here: https://thejs.blog/2019/07/04/node-js-api-gateway-a-developer-perspective/