GithubHelp home page GithubHelp logo

modules-middleware's Introduction

modules-middleware

NPM Version

Express middleware for serving node modules to client side.
Useful for serving client side apps structued as node module package, containing a package.json and dependencies.

modules-middleware:

  • Serves sources and static assets from the module directory
  • Serves sources and static assets of module dependencies defined in package.json (resolves using node's require.resolve starting from the module directory)
  • works with monorepos
  • transforms package names to valid browser ES6 paths when serving .js, .jsm and .html source files of the module and dependencies. for example, a js module containing an ES6 import such as:
    import { PolymerElement } from '@polymer/polymer'; will transform into
    import { PolymerElement } from '/node_modules/@polymer/polymer/';
    • Support node.js require() flexibility, (omitting .js extension, specifying directory name only to serve a contained index.js file, etc..)
    • Multiple packages sharing the same dependency at the same version will result in the same client url (for caching)
    • Multiple packages sharing the same dependency with different versions will result in a different client url

Install

$ npm install modules-middleware

or

$ yarn add modules-middleware

API

var modulesMiddleware = require('modules-middleware')

modulesMiddleware(modulePath, options)

Create a new middleware function to serve files from within a given module path.

Options

modulesDirectoryUrlName

The prefix of packages urls for transformed client side sources, defaults to "node_modules".

Serving using express

This is a simple example of using Express.

var express = require('express')
var modulesMiddleware = require('modules-middleware')

var app = express()

app.use(modulesMiddleware('./frontend'));
app.listen(3000)

with monorepo

if you are using a monorepo containing a package of your client side code you may use require.resolve as the module path

var express = require('express')
var modulesMiddleware = require('modules-middleware')

var app = express()

app.use(modulesMiddleware(require.resolve('my-client-app'));
app.listen(3000)

License

MIT

modules-middleware's People

Contributors

barakedry avatar

Watchers

 avatar  avatar

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.