Code Monkey home page Code Monkey logo

Comments (6)

jkyberneees avatar jkyberneees commented on June 7, 2024

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

Thanks and Regards

from fastify-gateway.

parthibd avatar parthibd commented on June 7, 2024
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}`)
});

from fastify-gateway.

parthibd avatar parthibd commented on June 7, 2024

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 ?

from fastify-gateway.

jkyberneees avatar jkyberneees commented on June 7, 2024

Hi @parthibd, I think this thread will be of your help: fastify/fastify#202

Thanks and Regards

from fastify-gateway.

parthibd avatar parthibd commented on June 7, 2024

I tried this solution, but it does not work.

from fastify-gateway.

jkyberneees avatar jkyberneees commented on June 7, 2024

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/

from fastify-gateway.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.