![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
express-view-switcher
Advanced tools
switch the view root directory per request
npm install -S express-view-switcher
import viewSwitcher from "express-view-switcher";
const app = express();
app.set("view engine", "pug");
app.use(viewSwitcher((req) =>
{
// enumerate the directories to be searched upon parsing "User-Agent" header
// ...and the first view that is found will be rendered
// 1. views/smartphone
// 2. views/default
return [["smartphone", "default"]];
}));
// ...then call res.render() as usual
app.use(viewSwitcher((req) =>
{
// parses "Accept-Language" headers, query strings, etc.
// 1. views/en-us
// 2. views/en
// 3. views/ja
return [["en-us", "en", "ja"]];
}));
app.use(viewSwitcher((req) =>
{
// can be combined!
// 1. views/en-us/smartphone
// 2. views/en-us/default
// 3. views/en/smartphone
// 4. views/en/default
// 5. views/ja/smartphone
// 6. views/ja/default
return [
["en-us", "en", "ja"],
["smartphone", "default"],
];
}));
// if you're using a template engine that supports inclusion or inheritance, and can specify and set the base directory root key name in res.locals,
// you can set this root key by passing it as the second argument
app.use(viewSwitcher((req) =>
{
return [["smartphone", "default"]];
}), "basedir"); // if using Pug as your template engine, there's no need to specify the root key name, as "basedir" is automatically set in res.locals
app.use(viewSwitcher((req) =>
{
return [getLanguageCandidates(req), getDeviceCandidates(req)];
}));
/**
* get request header
* @param {Object} req
* @param {string} name
* @param {int} maxLength
* @return {string}
*/
function getRequestHeader(req, name, maxLength = 256)
{
if(!req.headers.hasOwnProperty(name))
{
return "";
}
return req.headers[name].substr(0, maxLength);
}
/**
* list up language candidates
* @param {Object} req
* @return {string[]}
*/
function getLanguageCandidates(req)
{
const acceptLanguage = getRequestHeader(req, "accept-language");
const languageCandidates = [];
for(const language of acceptLanguage.split(","))
{
const parts = language.split(";");
languageCandidates.push(parts[0]);
}
// finally, push the default language
languageCandidates.push("en");
return languageCandidates;
}
/**
* list up device candidates
* @param {Object} req
* @return {string[]}
*/
function getDeviceCandidates(req)
{
// get device info from UserAgent
const userAgent = getRequestHeader(req, "user-agent");
return [detectDevice(userAgent), "default"];
}
/**
* detect device from UserAgent
* @param {string} userAgent
* @return {string}
* @private
*/
function detectDevice(userAgent)
{
if(userAgent.indexOf("iPhone") !== -1)
{
return "smartphone";
}
if(userAgent.indexOf("iPod touch") !== -1)
{
return "smartphone";
}
if(userAgent.indexOf("iPad") !== -1)
{
return "tablet";
}
if(userAgent.indexOf("Android") !== -1)
{
if(userAgent.indexOf("Mobile") !== -1)
{
return "smartphone";
}
else
{
return "tablet";
}
}
return "default";
}
See CHANGELOG.md.
[3.4.0] - 2020-07-06
FAQs
View switcher for Express.js
The npm package express-view-switcher receives a total of 1 weekly downloads. As such, express-view-switcher popularity was classified as not popular.
We found that express-view-switcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.