Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 72 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,105 @@
const express = require('express');
const path = require('path');
const helmet = require('helmet')
const express = require("express");
const path = require("path");
const helmet = require("helmet");
const url = require("url");

const app = express();
const NEXCHANGE_ROOT = process.env.NEXCHANGE_ROOT
const ICO_ROOT = process.env.ICO_ROOT
const NEXCHANGE_ROOT = process.env.NEXCHANGE_ROOT;
const ICO_ROOT = process.env.ICO_ROOT;

//Helmet helps you secure your Express apps by setting various HTTP headers.
app.use(helmet())
app.use('/ico', express.static(path.resolve(ICO_ROOT)));
app.use(express.static(path.resolve(NEXCHANGE_ROOT), {index: false}));
app.use(helmet());
app.use("/ico", express.static(path.resolve(ICO_ROOT)));
app.use(express.static(path.resolve(NEXCHANGE_ROOT), { index: false }));

function getCur(qParam) {
return qParam.toUpperCase().substr(-3);
return qParam.toUpperCase().substr(-3);
}

app.get('/ico', (req, res) => {
res.sendFile(path.resolve(ICO_ROOT, 'index.html'));
app.get("/ico", (req, res) => {
res.sendFile(path.resolve(ICO_ROOT, "index.html"));
});

// Handling lowercase order Ids
var orderUppercase = (req, res, next) => {

if (req.params.orderId) {
const orderIdUP = req.params.orderId.toUpperCase()
if (req.params.orderId != orderIdUP) {
res.redirect('/order/' + orderIdUP)
}
else {
next()
}
const orderUppercase = (req, res, next) => {
const orderIdUP = req.params.orderId.toUpperCase();
if (req.params.orderId !== orderIdUP) {
res.redirect(`/${lang}/order/${orderIdUP}`);
} else {
next();
}
};

// General handler for the rest of the URLs
var generalHandler = (req, res) => {
const generalHandler = (req, res) => {
let host = undefined;
let urlPath = req.path;
const langInPath = urlPath.split("/")[1];
const langInParam = req.query.lang ? req.query.lang.toLowerCase() : undefined;
let lang = langInPath || langInParam || "en";
const fromCurr = req.query.cur_from;
const toCurr = req.query.cur_to;
const validLanguages = ["en", "de", "ru"];

let redirectRequired = false;
let params = {};
if (req.query.cur_from && req.query.cur_to) {

// Dont add language path in url, if its not n.exchange
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if some new whitelabels come that use our lang param?

However whitelabels generally do not get traffic from bestchange

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client side routing will be used

Copy link
Author

@punitm0 punitm0 May 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced it with const languageRedirect = true;
when set to true, language will be added in url path while redirecting

if (
!req.headers.host.includes("n.exchange") &&
!req.headers.host.includes("localhost")
)
lang = "";

if (validLanguages.includes(langInPath)) {
res.header("Set-Cookie", `i18next=${lang}`);

urlPath = urlPath.substr(lang.length + 1);

// If lang is in path then ignore lang in param
delete req.query["lang"];
}

if (fromCurr && toCurr) {
const fromCurrType = fromCurr.substr(0, fromCurr.length - 3);

if (["ADVC", "PR"].includes(fromCurrType)) host = "s.api.n.exchange";

console.log(fromCurrType);
req.query.pair = getCur(toCurr) + getCur(fromCurr);
delete req.query.cur_from;
delete req.query.cur_to;
redirectRequired = true;
req.query.pair = getCur(req.query.cur_to) + getCur(req.query.cur_from);
delete req.query['cur_to'];
delete req.query['cur_from'];
}

if (req.query.lang && req.query.lang !== req.query.lang.toLowerCase()) {
if (req.query.lang && validLanguages.includes(langInParam)) {
delete req.query["lang"];
redirectRequired = true;
req.query.lang = req.query.lang.toLowerCase()
}

if (redirectRequired) {
params = req.query;
params = Object.keys(params).map(key => key + '=' + params[key]).join('&');
res.redirect(req.path + '?' + params);
const redirectUrl = url.format({
pathname: urlPath === "/" ? `/${lang}` : `/${lang}${urlPath}`,
query: req.query,
});

if (host) {
res.redirect(`https://${host}${redirectUrl}`);
return;
}

res.redirect(redirectUrl);
return;
}

res.sendFile(path.resolve(process.env.NEXCHANGE_ROOT, 'index.html'));
res.sendFile(path.resolve(process.env.NEXCHANGE_ROOT, "index.html"));
};

// Convert order ids to uppercase
app.get('/order/:orderId', [orderUppercase, generalHandler]
);
app.get("/order/:orderId", [orderUppercase, generalHandler]);

// For all other cases
app.get('*', generalHandler);


app.listen(3000, () => console.log('Nexchange Frontend Proxy is listening on port 3000!'));

app.get("*", generalHandler);

app.listen(3000, () =>
console.log("Nexchange Frontend Proxy is listening on port 3000!")
);