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-moduledev

v1.0.25

Published

按模块划分目录,每个模块目录可拥有独立的controller、service、model、static等,底层采用了Express做为基础框架

Downloads

8

Readme

说明

基础Express做的模块化开发框架,按模块划分业务,支持模块内视图和路由配置,每个模块目录可拥有独立的controller、service、model、static等,这样可以让负责不同业务模块的开发人员更关注业务本身。

安装

npm i express-moduledev

项目目录结构 [方括号为可选目录]

app // 项目主目录
│  |─ modules    //模块目录
│  │  |─ module_A   //业务模块A
│  │  │  |─ ctrls 控制器文件
│  │  │  |  └─ controller1.js
│  │  │  |─ views 视图模板文件
│  │  │  |  └─ index.html
│  │  │  |─ [static]    //静态文件等
│  │  │  |─ [models]    //数据处理文件
│  │  │  |─ [servs]     //服务层 业务处理
│  │  │  └─ [router.js] //模块的路由配置文件
│  │  |── module_B 模块B
│  |─ [models]     //ORM 数据库模型定义文件(放在顶级,表示可通用)
│  |─ [routes]     //通用路由器配置
│  |─ [views]      //通用视图模板文件存放位置
│  └─ [ctrls]      //通用控制器文件
├─ config  //环境配置目录
│   |─ default.js   //默认配置文件
│   |─ [development.js]  //开发环境配置文件
│   |─ [production.js]   //生产环境配置文件
│   └─ [testing.js]      //测试环境配置
└─ index.js         //启动文件

在您的项目根目录,新建启动文件index.js(或其它名字)

//index.js
var em = require('express-moduledev');

var config = {
    //指定端口,默认端口 8000
    //"port":80,
    //使用环境 'default','development','production','testing'
    "use_env": "default",
    //顶级路由存放目录名称 默认routes
    //"router":"routes/",
}

//启动服务
em.Run(config)
em.log.p('server runing..')

配置文件

//这里使用了类的形式来定义配置
//config/default.js
var session = require('express-session');
var FileStore = require('session-file-store')(session)
//var em = require('express-moduledev');
//var FileStore = require('session-file-store')(em.session)

class Config{
    constructor(){
        //是否开启调试模式
        this.debug = false;
        //数据库连接配置
        this.database = {
            "host": "127.0.0.1",
            "port": "3306",
            "database": "test",
            "username": "root",
            "password": "root",
            "dialect": "mysql"
        };

        //配置session_store
        this.session_store = {
            "secret": 'skdf093ks',
            "cookie": {  maxAge: 1000 * 60 * 60 * 24},
            "resave": true,
            "saveUninitialized": false,
            "store":new FileStore
        };

        //配置日志信息
        //this.log4js = false //关闭日志
        this.log4js = {
            appenders: {
                out: {
                    type: 'stdout'
                },
                app: {
                    type: "dateFile",
                    filename: 'logs/log',
                    pattern: "_yyyyMMdd.log",
                    alwaysIncludePattern: true,
                    maxLogSize: 20480,
                    backups: 3,
                }
            },
            categories: {
                default: { appenders: ['out','app'], level: 'info' }
            }
        }
    }
}

module.exports = Config;

//开发环境文件
//config/development.js
var session = require('express-session');
var RedisStore = require('connect-redis')(session)

var Config = require('./default');
//这里需要继承Config
class Development extends Config{
    constructor(){
        super();
        this.debug = true;
         this.database = {
             ....
         }
        //配置session_store
        this.session_store = {
            "secret": 'skdf093ks',
            "cookie": {  maxAge: 1000 * 60 * 60 * 24 },
            "resave": true,
            "saveUninitialized": false,
            "store":new RedisStore({
                "host": '127.0.0.1',
                "port": '6300'
            })
        }
    }
}
module.exports = Development;

...

可以通过 em.config 获取配置选项

桥接文件

有时我们需要加载自己的通用函数库,或者对模板进行扩展,可以通过桥接文件实现,在 app下新建bridge目录,该目录下的文件会自动加载(支持多目录多文件)

//app/bridge/templateExt.js

//对nunjucks模板扩展方法
module.exports.tpl  = { //这里的 tpl 名字可以自己定义

    //init将自动执行
    init (app){
        //获取配置信息
        let envcfg = app.config.getEnv();
        //获取模板对象
        let tpl = app.get('tpl');
        //添加一个模板全局变量
        tpl.addGlobal("BaseUrl", envcfg.BaseUrl)
        //添加一个日期过滤器
        tpl.addFilter('formatTimestamp', function(t, f="yyyy-MM-dd HH:mm:ss"){
            return new Date(t*1000).pattern(f)
        })
    }
}

//app/bridge/Comm.js
//通用库文件
module.exports.comm = {
    test (){ }
}

//通过全局APP调用方法
APP.comm.test()

版本更新

  • 1.0.7 支持modules下再分modules
  • 1.0.8 修改modules下再分modules的部分bug
  • 1.0.9 添加DB SQL防注入功能
  • 1.0.10 新增自定义函数库加载,位于app/bridge目录下
  • 1.0.11 支持log4js日志配置功能
  • 1.0.12 优化数据库加载
  • 1.0.13 修复模块加载时指定静态资源目录无法加载的问题
  • 1.0.15 修复sequelize兼容问题
  • 1.0.16 修复在子目录启动项目时,加载配置文件会读取根目录导致加载失败的问题
  • 1.0.18 支持配置文件设置静态目录 static_folder
  • 1.0.21 去掉根据文件路径获取模块名,modules里的路由应该写到modules里去
  • 1.0.22 添加路由配置允许设定是否跳过csrf检测
  • 1.0.24 把检测csrf的方法放移到了router.js里
    //设置chkcsrf为flase表示跳过csrf检测,限POST方法
    { prefix:"/test",  ctrl:"test", action:"test" , method:["GET", "POST"],"chkcsrf": false}
  • 1.0.25 修复在路由配置中,如果指定url_prefix前缀,prefix无法设置数组的bug
    exports.url_prefix = "/abc"
    //prefix 可以使用数组定义 ,路由可以命中 /abc 也可以命中 /abc/index.html
    { prefix: ["/","/index.html"],  ctrl:"test", action:"test"}

开源地址

https://github.com/rob668/express-moduledev

在线DEMO

http://www.robweb.cn/demos/express-moduledev-demo

DEMO源码

https://github.com/rob668/express-moduledev-demo