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-json-server

v0.0.2

Published

express json server

Downloads

9

Readme

Express json server

基于Express的静态json数据服务

目录

Overview

概览

基于express和json-server组合起来并制定了一些规则从而更好更快的嵌入应用中使用

Install

安装

$ npm i [-S] express-json-server

Useage

使用

// app.js
const path = require('path');

const express = require('express');
const autoController = require('express-json-server');

const app = express();

// 配置db路径
app.autoController(path.join(__dirname, 'db'));

// 另一种运行方式
// autoController(app, path.join(__dirname, 'db'));

app.listen(3000);

Configure

配置

// db/index.js
module.exports = {
  delay: 500,                 // 响应延迟(ms), 可选, 默认0
  // foreignKeySuffix: 'Id',  // 外键后缀, 可选, 默认'Id', 'userId'则外键关联user表的id,
  // id: 'id',                // 数据id, 可选, 默认'id'
  json: 'db.json',            // json数据存储到文件中, 可选, 默认直接返回js生成的动态数据
  // jsonSpaces: 2,           // json缩进空格数, 可选, 默认2
  // middlewares: [],         // 中间件列表, 可选
  // noCors: false,           // 禁止跨域访问, 可选, 默认false不禁止
  // noGzip: false,           // 禁止压缩传输, 可选, 默认false不禁止
  // readOnly: false,         // 只读模式, 可选, 默认false非只读
  // route: '/api',           // 挂载到express下的路由, 可选, 默认'/api', 则访问路径为'/api/${table}'

  // 路由别名, 可选
  routes: {
    '/good': '/product',
    '/member': '/user',
  },

  // 表数据, 必须
  tables: {
    product: require('./product'),
    user: require('./user'),
  },

};

Sample

范例

$ node app.js

# curl -X GET localhost:3000/api/user -> GET /
[
  {
    "id": 1,
    "name": "魏8",
    "email": "[email protected]"
  },
  {
    "id": 2,
    "name": "余_鑫鹏",
    "email": "[email protected]"
  },
  {
    "id": 3,
    "name": "钱83",
    "email": "[email protected]"
  },
  {
    "id": 4,
    "name": "杜.航19",
    "email": "[email protected]"
  },
  {
    "id": 5,
    "name": "白.耀杰",
    "email": "[email protected]"
  },
  {
    "id": 6,
    "name": "石73",
    "email": "[email protected]"
  },
  {
    "id": 7,
    "name": "田.越彬",
    "email": "[email protected]"
  },
  {
    "id": 8,
    "name": "徐34",
    "email": "[email protected]"
  },
  {
    "id": 9,
    "name": "金44",
    "email": "[email protected]"
  },
  {
    "id": 10,
    "name": "洪24",
    "email": "[email protected]"
  }
]

# curl -X GET localhost:3000/api/user/1 -> GET /
{
  "id": 1,
  "name": "魏8",
  "email": "[email protected]"
}

# curl -X GET localhost:3000/api/user?name_like=4 -> GET /
[
  {
    "id": 8,
    "name": "徐34",
    "email": "[email protected]"
  },
  {
    "id": 9,
    "name": "金44",
    "email": "[email protected]"
  },
  {
    "id": 10,
    "name": "洪24",
    "email": "[email protected]"
  }
]

更多请求方式请参考json-server文档

License

MIT - xiewulong