Git Product home page Git Product logo

express-json-file-crud's Introduction

This package helps to quickly setup a CRUD REST service using Express: You can create, read, update and delete objects using REST.

It stores data in plain JSON files. No database is required, which makes setup rapid compared to similar solutions, and therefore perfectly suitable for quickly prototyping solutions.

This package should probably not be used for productive applications, as the underlying JSON-file-based storage engine is very basic and not a replacement for a proper database.

Setup

In your express project, run npm install --save express-json-file-crud.

Then mount a CRUD route under any endpoint, e.g.:

const makeCrud = require('express-json-file-crud').makeCrud;
const carCrud = makeCrud('cars', './storage');
app.use('/cars', carCrud);

makeCrud(entityName: string, storagePath: string) takes two arguments:

  • entityName: The type of entity that will be stored using this end point.
  • storagePath: The (relative or absolute) path to a directory where one file per entity will be stored.

All entities will be stored in [storagePath]/[entityName].json. The individual [entityName].json files and storagePath will be created if they don't exist.

IDs

Objects are identified by a numeric ID. When an object is created that has no ID set or that has an ID that conflicts with an existing ID, the id property will automatically be filled (by incrementing the highest stored ID so far for the given entity).

The IDs are used to read, update and delete specific objects.

REST usage

Action Path HTTP Action Description
Create / POST
Read (list all objects) / GET Returns an array of all stored entities
Read (single object) /:id GET Returns the object with the given ID
Update (update existing object) /:id PUT Updates the object with the given ID
Delete /:id DELETE deletes the object with the given ID

When you start your express app, the cars route is now ready to be used.

POST and PUT need to contain a request header Content-Type: application/json to work properly.

For example, to create a new car, you can call

POST /cars

with a car object as the request body.

To list all cars, you can call

GET /cars

and get an array with cars back as the response body.

To get the car with the ID 1, you can call

GET /cars/1

To update it, call

PUT /cars/1

with the updated car object in the request body.

To delete the car with the ID 1:

DELETE /cars/1

express-json-file-crud's People

Contributors

fchristl avatar

Watchers

 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.