Giter VIP home page Giter VIP logo

simple-api-mock's Introduction

A simple API mock using express.js, for faster UI development.

Features:

  • Has a simple DB kept in memory, and sync it to db.json, so you can reload your app without losing your state
  • Hot reload
  • Automatically logs info about requests and response
  • Can live watch the DB content
  • Customize index.js via express to your needs

How to use:

$ npm install
$ npm start  // Starts on localhost:3333, has hot-reload on index.js and db.json

$ npm run lint
$ npm run watch:db  // Live print of the DB content

Examples for fast prototyping:

// GET
app.get('/api/data/:id', async (req, res) => {
  // Example for getting params from path
  const id = req.params.id;

  // Extract data from headers
  const customHeader = req.headers['custom-header'];

  // Extract data from query string
  const page = req.query.page;

  console.log(customHeader, page);

  if (db[id]) {
    res.json(db[id]);
  } else {
    res.status(404).json({ error: 'Data not found' });
  }
});

// POST
app.post('/api/data', async (req, res) => {
  const data = req.body; // Example for gettings params from body
  //
  const id = data.id;
  db[id] = data;

  await syncJsonDb();
  res.status(201).json({ message: 'Data created' });
});

// PUT
app.put('/api/data/:id', async (req, res) => {
  const id = req.params.id;
  const data = req.body;

  db[id] = data;

  await syncJsonDb();
  res.status(200).json({ message: 'Data updated' });
});

// PATCH
app.patch('/api/data/:id', async (req, res) => {
  const id = req.params.id;
  const updates = req.body;

  if (db[id]) {
    db[id] = { ...db[id], ...updates };

    await syncJsonDb();
    res.status(200).json({ message: 'Data updated' });
  } else {
    res.status(404).json({ error: 'Data not found' });
  }
});

// DELETE
app.delete('/api/data/:id', async (req, res) => {
  const id = req.params.id;

  if (db[id]) {
    delete db[id];

    await syncJsonDb();
    res.status(200).json({ message: 'Data deleted' });
  } else {
    res.status(404).json({ error: 'Data not found' });
  }
});

simple-api-mock's People

Contributors

brunogsa avatar

Stargazers

Guilherme Salomão avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.