@aquestsrl/mock-api

1.1.2 • Public • Published

@aquestsrl/mock-api

A simple mock api server

Getting started

npm i @aquestsrl/mock-api

or

yarn add @aquestsrl/mock-api

Create mock-api.config.js on your project root:

const path= = require('path');

module.exports = () => ({
  address: 4002,
  publicPath: 'path-to-public-folder',
  routes: [ 
    {
      route: '/api/hp',
      module: path.resolve(__dirname, 'mock/routes/foo'),
    },
  ],
});

Example route module

module.exports = ({
  shuffleArray,
  shuffleIndexArray,
  humanize,
}) => (req, res) => res.json({
  foo: 'bar',
  image: '/logo.png',
});

Usage from CLI

mock-api

Usage as middleware

const Express = require('express');
const mock = require('@aquestsrl/mock-api');

const App = new Express();

App
  .use(mock())
  .listen(3000);

Bundle with Webpack

If you need to bundle data with Webpack, after create the middleware add MockApiPlugin to your webpck configuration: this plugin create an Express application based on your mock-api.config.js, ready to be bundled. If provided, contents of public folder are copied to toPublicPath destination.

const path = require('path');
const MockApiPlugin = require('@aquestsrl/mock-api/webpack/plugin');

const config = {
  ...,
  plugins: [
    new MockApiPlugin({
      configPath: path.resolve(__dirname, 'mock-api.config.js'),
      toPublicPath: '/public/api', // relative to dist folder
    }),
  ],
};

Readme

Keywords

none

Package Sidebar

Install

npm i @aquestsrl/mock-api

Weekly Downloads

1

Version

1.1.2

License

ISC

Unpacked Size

82 kB

Total Files

8

Last publish

Collaborators

  • aquestdigital