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-routeless-mvc

v1.0.1

Published

Express middleware for dynamic, file-based routing without explicit route definitions, supporting flexible MVC structures.

Downloads

6

Readme

express-routeless-mvc

⚠️ Experimental - Under Development ⚠️

Express middleware for dynamic, file-based routing without explicit route definitions, supporting flexible MVC structures. Controller and view names must currently match.

Installation

npm install express-routeless-mvc

Usage

const express = require('express');
const setupRoutelessMVC = require('express-routeless-mvc');
const path = require('path');

const app = express();

// Register compilers if needed (e.g., CoffeeScript, TypeScript)
// Setup your static files ('public', favicon, etc.)
// Setup other middleware (logger, bodyParser, etc.)

// Example where it is set as the defaults
setupRoutelessMVC(app, {
  viewsDir: path.join(__dirname, 'views'),         // Default: 'views'
  controllersDir: path.join(__dirname, 'controllers'), // Default: 'controllers'
  viewEngine: 'html',                              // Default: 'html'
  viewExtensions: ['.html'],                       // Default: ['.html']
  controllerExtensions: ['.js'],                   // Default: ['.js']
  caseSensitive: false,                            // Default: false
});


app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Features

  • Dynamic Routing: Automatically maps routes based on the views directory structure.
  • Flexible MVC: Organize controllers and views without manual route definitions.

Options

  • viewsDir (String): Directory for view templates. Default: 'views'.
  • controllersDir (String): Directory for controllers. Default: 'controllers'.
  • viewEngine (String): Template engine to use. Default: 'html'.
  • viewExtensions (Array): Supported view file extensions. Default: ['.html'].
  • controllerExtensions (Array): Supported controller file extensions. Default: ['.js'].
  • caseSensitive (boolean): Sets the file name case sensitivity. Default: 'false'.

Important

  • Automatic Routing: This package automatically routes all files in the viewsDir to corresponding routes. If you need to restrict access to certain routes (e.g., require authentication), you must implement your own authorization solution.

  • Authorization: It is standard practice to handle authentication and authorization separately, typically via middleware or in your controllers. You can use packages like Passport.js or add custom authorization logic inside your controllers, such as:

    // controllers/admin.js
    module.exports = (req, res, next) => {
        if (!req.user) return res.status(401).send('Unauthorized');
        if (req.user.role !== 'admin') return res.status(403).send('Forbidden');
        return { title: 'Admin Dashboard', user: req.user };
    };
  • Naming Convention: Controller and view filenames must match to associate them with the same route.

    • Example: For route /about, use views/about.html and controllers/about.js.

Controllers

Optional functions that provide data to views.

Example: controllers/index.js

module.exports = (req, res, next) => ({
  title: 'Home Page',
  message: 'Welcome to our website!'
});

Views

Place templates in the viewsDir. Filename determines the route.

Example: views/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to our website!</h1>
</body>
</html>

Error Handling

Add middleware to handle 404 and other errors.

// 404 Handler
app.use((req, res) => {
  res.status(404).sendFile(path.join(__dirname, 'views', '404.html'));
});

// Error Handler
app.use((err, req, res, next) => {
  res.status(err.status || 500).send('Internal Server Error');
});

Notes

  • Compilers: Register necessary compilers before setting up the middleware.

    // For CoffeeScript
    require('coffeescript/register');
    
    // For TypeScript
    require('ts-node/register');
  • Changing View Engine:

    // Install Pug
    npm install pug
    
    // Setup with Pug
    setupRoutelessMVC(app, {
      viewEngine: 'pug',
      viewExtensions: ['.pug'],
    });

License

MIT