json-server-setup

1.0.4 • Public • Published

A simple JSON Server Setup

This file sets up a lightweight REST API using json-server. It reads data from a db.json file and serves it over HTTP. Perfect for prototyping, testing, or mocking APIs.

Usage:

On the terminal, run the following commands:

npx json-server-setup db.json 5000

With cors

npx json-server-setup db.json 5000 --cors-origin "http://localhost:3000"

Or install it globally:

npm install -g json-server-setup

Then run:

json-server-setup db.json 5000

or with cors origin:

json-server-setup db.json 5000 --cors-origin "http://localhost:3000"

As a library:

const createJsonServer = require('json-server-setup')

createJsonServer('db.json', 5000, {
  origin: 'http://localhost:3000',
  methods: 'GET, POST, PUT, DELETE',
  allowedHeaders: 'Content-Type, Authorization'
})

In ReactJS:

import { useJsonServer } from 'json-server-setup'

function MyComponent() {
  const { isRunning, error, data, startServer, stopServer, baseUrl } =
    useJsonServer({
      dbFile: './data/db.json',
      port: 3001,
      autoStart: true
    })

  if (error) {
    return <div>Error: {error.message}</div>
  }

  return (
    <div>
      <div>Server Status: {isRunning ? 'Running' : 'Stopped'}</div>
      <div>Server URL: {baseUrl}</div>

      <button onClick={startServer}>Start Server</button>
      <button onClick={stopServer}>Stop Server</button>

      {data && <pre>{JSON.stringify(data, null, 2)}</pre>}
    </div>
  )
}

Package Sidebar

Install

npm i json-server-setup

Weekly Downloads

15

Version

1.0.4

License

MIT

Unpacked Size

11.2 kB

Total Files

5

Last publish

Collaborators

  • wathika-eng