npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-view-switcher

v3.4.0

Published

View switcher for Express.js

Downloads

6

Readme

express-view-switcher

Build Status (Windows) Build Status (macOS) Build Status (Linux) Syntax check Release Node.js version License

リクエストごとにビューのルートディレクトリを切り替え

インストール

npm install -S express-view-switcher

使い方

1: マルチデバイス対応

import viewSwitcher from "express-view-switcher";

const app = express();
app.set("view engine", "pug");
app.use(viewSwitcher((req) =>
{
    // "User-Agent" ヘッダを解析して検索したいディレクトリを列挙
    // 最初に見つかったビューを表示
    // 1. views/smartphone
    // 2. views/default
    return [["smartphone", "default"]];
}));

// あとは普通に res.render() をコール

2: 多言語対応

app.use(viewSwitcher((req) =>
{
    // "Accept-Language" ヘッダやクエリストリングなどを解析
    // 1. views/en-us
    // 2. views/en
    // 3. views/ja
    return [["en-us", "en", "ja"]];
}));

3: 多言語+マルチデバイス

app.use(viewSwitcher((req) =>
{
    // 組み合わせてもOK
    // 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"],
    ];
}));

ベースディレクトリを指定

// 包含や継承をサポートしているテンプレートエンジンで、res.localsにベースディレクトリを指定できる場合は第2引数にキー名を指定する
app.use(viewSwitcher((req) =>
{
    return [["smartphone", "default"]];
}), "basedir"); // テンプレートエンジンにPugを使っている場合、自動的にres.localsにbasedirが追加されるのでこの指定は不要

実践的な例

app.use(viewSwitcher((req) =>
{
    return [getLanguageCandidates(req), getDeviceCandidates(req)];
}));

/**
 * リクエストヘッダを取得
 * @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);
}

/**
 * 言語候補を取得
 * @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]);
    }

    // 最後にデフォルトの言語を追加
    languageCandidates.push("en");
    return languageCandidates;
}

/**
 * デバイス候補を取得
 * @param {Object} req
 * @return {string[]}
 */
function getDeviceCandidates(req)
{
    // UserAgentからデバイス情報を取得
    const userAgent = getRequestHeader(req, "user-agent");
    return [detectDevice(userAgent), "default"];
}

/**
 * User-Agentからデバイスを検出
 * @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";
}

変更履歴

CHANGELOG.mdを参照ください。