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

edaoe-cli

v1.1.1

Published

## 介绍 edaoe命令行工具,用于快速生成node服务项目。

Downloads

365

Readme

edaoe-cli

介绍

edaoe命令行工具,用于快速生成node服务项目。

优势

  • 静态服务 支持根目录/文件路由
  • 内置常用方法
  • 文件路由 大多数情况下你只需要在controllers目录下工作,保持专注
  • 路由拦截
  • 快速配置跨域
  • 快速配置rewrite
  • 高内聚,低耦合

快速开始

安装

npm i edaoe-cli -g

生成项目

你首先需要建立一个文件夹或者新建一个git仓库 clone之后进入到文件夹下执行命令,例如你新建一个test文件夹

cd test
edaoe

安装项目依赖

npm i

启动项目

在启动之前你需要关注两点:

  • 查看configs/app.js 端口号是否可以正常启动 保证不被占用
  • 查看configs/sequlize中配置的数据库连接,这里是我自己电脑的本地的配置,大概率你的是不一样的 那么同样的在model中的User模型 你也是无法建立的,如果你有本地数据库 你需要正确的配置,如果你没有 你需要将configs/sequlize.js中 更改配置 导出为空即可, 同时删除model中的User;

在检查并正确配置上述两点后你可以执行下面的命令

npm run dev

访问 http://localhost:3002/index

恭喜🎉🎉🎉 项目已经跑起来了


接口

最简单直接的有可能是你需要一个node接口,这里在controllers/test/目录下有get请求示例,在浏览器中打开http://localhost:3002/test/get?word=a&language=Chinese,

是的,文件的路径就是你可以访问的node接口,在大多数情况下,你只需要在controllers目录下复制粘贴,更改文件名形成新的node接口,这可能也是你日后绝大多数的需要做的工作 😏😏😏

开始第一个接口

假设你有这样一个请求:

转发请求

前端请求--> node(cc/get) --> php服务 | | 前端页面 <-- node <--

  • 确保端口号可以正常启动,如果被占用 你可能需要去configs/app.js中修改可以监听的端口号
  • 在controllers目录下新建test/get.js 本例中已存在
  • 修改configs/fetch中的baseUrl 此处的baseUrl是其他的http接口,示例中是http://0.0.0.0:3008 记得修改哦

rewrite规则

在configs/rewrite.js中配置rewrite规则 参考文档:https://github.com/koajs/rewrite#readme

{
  from: /^\/i(\w+)/, to: '/items/$1'
}

注意这个在单页面应用中是一定需要配置的,不然会出现刷新页面404的情况哦

全局变量app

koa app暴露给用户,方便用于用于挂载一些方法,变量等 使用方法:

// 在app目录下
module.exports = function(app) {
  app.cc = 1;
}

从数据库中取得数据 (暂时只支持mysql, redis) 以mysql 为例

mysql目前通过sequelize支持

  • 检查configs/sequelize 实例配置
const Sequelize = require('sequelize');
const test = new Sequelize('test', 'root', 'xl123456', {
  host: '127.0.0.1',
  port: 3306,
  dialect: 'mysql'/* 选择 'mysql' | 'mariadb' | 'postgres' | 'mssql' 其一 */
})
exports.defaultConfig = {
  // sequelize实例基础配置 会被挂载到app.db.sequelize上面
  test: () => test
}
  • model中配置模型 定义一个User模型
const { DataTypes } = require("sequelize");
module.exports = class User {
  constructor() {
    const sequelize = app.db.sequelize.test(); // sequeLize实例
     // 强制文件名 类名 定义模型名三者一致
    this.model = sequelize.define('User', {
      // 在这里定义模型属性
      firstName: {
        type: DataTypes.STRING,
        allowNull: false
      },
      lastName: {
        type: DataTypes.STRING
        // allowNull 默认为 true
      }
    }, {
      // 这是其他模型参数
      freezeTableName: true,
      timestamps: false
    });
    this.model.sync();
  }
  cc(firstName, lastName) {
    return this.model.create({ firstName, lastName })
  }
}
  • 调用 可以在任何地方调用app.db.Model.User.cc方法

进阶

目录说明

.
├── controllers // 存放所有的路由文件 也就是node接口
│   ├── cc
│   ├── index.js
│   └── test
├── assets //存放所有自定义的文件 例如一些常量 枚举值
│   ├── const.js
│   └── readme.md
├── app //暴露的app变量 方便用户挂载
│   ├── rainBow.js  // app上挂载变量
├── configs //存放所有的配置文件 慎改慎删♥️
│   ├── controller.js  // 路由处理拦截等配置
│   ├── app.js
│   ├── env.js
│   ├── fetch.js  // 请求参数统一配置
│   ├── rewrite.js // rewrite规则
│   ├── cors.js // 跨域配置
│   ├── db.js // 数据库配置
│   ├── login.js // services/login的配置
│   └── test.js // services/test的配置
├── index.js // 启动入口
├── package.json
├── services // services层 这里你可以做一些逻辑 例如登陆鉴权等
│   ├── login.js
│   └── test.js
├── statics // 静态文件目录 
│   ├── a.txt
│   ├── cc
│   └── images
└── views // 存放的页面
    ├── index.ejs
    └── pages

configs

  • controller.js // 路由的配置文件 路由拦截 等 你都可以在这里做全局统一配置 需要说明的是,在真正执行过程中针对某一个路由的具体配置会覆盖这里的全局配置 例如在configs/controller.js中配置needlogin:true, 如果具体的路由如cc/get中配置为false 那么最终的cc/get的路由needlogin:false,但是不会影响其他路由默认你值依然needlogin:true
  • app.js 这里配置监听的端口号等应用信息
  • fetch.js 这里是ctx.fetch的默认配置,具体的ctx.fetch配置会merge 这里的configs/fetch.js
  • env.js 环境变量
  • 本例中的login.js和test.js分别是service中的config配置
  • rewrite.js 参考文档:https://github.com/koajs/rewrite#readme 使用方法见上面rewrite规则
  • cors.js 跨域配置
  • db.js 数据库配置 支持mysql, redis。 配置参见已有的db.js 使用在controllers/cc 目录下

app

目前你可以在全局任何地方直接取得app,但是如果你想在app上挂载变量我们约定在app目录下,并且可以保证的是在该目录下给app挂载变量,可以在其他任何文件中获取。因为在edaoe内部,这个是最先被读取的。使用demo见: 赋值: app/rainBow.js 取值:在任何文件下 app.xx

tips: 十分建议您在module.exports内部获取app上的变量,防止因异步挂载而取不到值的情况

services

这里你可以做一些逻辑 例如登陆鉴权等 在services目录下新建login.js

// login.js

async function checkLogin(ctx, config) {
  // 校验登录
  const { data: login_res } = await ctx.fetch({
    baseUrl: `host地址`,
    uri: `/xxx`,
    handleResponse: (data) => {
      console.log(data);
      return data;
    }
  })
  if(login_res && login_res.login) {
    return true
  } else {
    ctx.status = 301;
    // 重定向到登录地址
    ctx.redirect(`https:www.baidu.com`)
  }
  // return true  返回true表示登录成功
  // 如果没有登录 就去跳转登录
}

module.exports = config => {
  // 这里的config 就是services目录下的login.js中的配置
  return (ctx, next) => {
    ctx.checkLogin = ctx.request.checkLogin = checkLogin.bind(null, ctx, config)
    return next();
  }
}

在每一次请求前都需要校验 你需要在configs/controller中统一配置路由拦截 beforeHandler 中执行 ctx.checkLogin方法

statics

这里是静态文件的访问目录, 你可以这样访问:localhost:3002/statics/a.txt 也可以这样localhost:3002/a.txt 这样直接访问,这在某些文件校验的场合比较有用

views

这是前端页面文件 也是通过路由访问localhost:3002/index 推荐:如果你用react或者vue前端框架开发,打包成单页面应用,打包后的index.html文件 改成index.ejs 放入到该文件下 ♥️♥️♥️

ctx.fetch

ctx.fetch是完全基于node-fetch的封装 https://github.com/node-fetch/node-fetch, 但是有一下两点需要注意:

  1. ctx.fetch会将参数中的uri和configs/fetch.js中的baseUrl拼接在一起形成node-fetch中的第一个参数url
  2. 所有参数都需要放在body中 ctx.fetch会自动将get请求的参数拼接到url中
  3. ctx.fetch中的baseUrl优先级高于configs/fetch.js中的baseUrl
  4. configs/fetch.js中的handleResponse必须是一个函数,且需要有返回给ctx.fetch()执行后的值 常见handleResponse: (ctx, res) => { return res.json()} res.json()是来源于node-fetch的方法, 如果handleResponse中没有处理,则需要自己在具体的路由中处理ctx.fetch的返回值,同理ctx.fetch中的handleResponse优先级高于configs/fetch.js中的

post的请求示例, get请求:在浏览器中打开http://localhost:3002/cc/get?word=a&language=Chinese, 你应该能看到如下界面 post请求:你可能需要在前端发送post请求或者你需要在postman中发送post请求http://localhost:3002/cc/post

记得修改configs/fetch.js中的baseURL为你自己的 ♥️