How to use the json-server.create function in json-server

To help you get started, we’ve selected a few json-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rohan-deshpande / trux / test / server.js View on Github external
const jsonServer = require('json-server'); // eslint-disable-line no-undef
const middlewares = jsonServer.defaults();
const server = jsonServer.create();
const protocol = 'http';
const host = 'localhost';
const port = 3000;
const endpoint = `${protocol}://${host}:${port}`;
const schema = {
  'posts': [
    {
      'title': 'baz',
      'author': 'foo',
      'id': 1
    },
    {
      'title': 'qux',
      'author': 'bar',
      'id': 1
    }
github nuragic / marionette-bootstrap / server / api / server.js View on Github external
var jsonServer = require('json-server')

var server = jsonServer.create() // Returns an Express server
var router = jsonServer.router(__dirname + '/db.json') // Returns an Express router

server.use(jsonServer.defaults) // logger, static and cors middlewares
server.use(router) // Mount router on '/'

module.exports = server;
github magma / magma / nms / app / packages / magmalte / scripts / mockServer.js View on Github external
* See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @flow
 * @format
 */

const jsonServer = require('json-server');
const https = require('https');
const fs = require('fs');

const certFile = process.env.API_CERT_FILENAME ?? '.cache/mock_server.cert';
const keyFile =
  process.env.API_PRIVATE_KEY_FILENAME ?? '.cache/mock_server.key';

const server = jsonServer.create();
const router = jsonServer.router('./mock/db.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);

const buffer = fs.readFileSync('./mock/db.json', 'utf-8');
const db = JSON.parse(buffer);

// Add feg and feg_lte handlers
server.post('/magma/v1/feg', (req, res) => {
  if (req.method === 'POST') {
    res.status(200).jsonp('Success');
  }
});

server.post('/magma/v1/feg_lte', (req, res) => {
  if (req.method === 'POST') {
github silence717 / AngularJs1.x-gulp-seed / server.js View on Github external
/**
 * @author  https://github.com/silence717
 * @date on 2016/10/17
 */
var jsonServer = require('json-server');
var server = jsonServer.create();
var middlewares = jsonServer.defaults();
var bodyParser = require('body-parser');
var mockRouter = require('./mock/index');

// 添加默认的中间件 logger, static, cors and no-cache
server.use(middlewares);

// 解析 body
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({
	extended: false
}));

server.use(mockRouter);

server.listen(4001, function() {
github bucharest-gold / roi / test / fake-server.js View on Github external
static create (port) {
    const server = jsonServer.create();
    const router = jsonServer.router(this.createDb());
    server.use(jsonServer.defaults());
    server.use(router);
    const s = server.listen(port);
    return s;
  }
}
github ston3o / rdcli / test / testServer.js View on Github external
export default () => {
    const server = jsonServer.create();
    server.use(bodyParser.json());

    const defaultRoute = server._router.stack.slice();
    server.reset = () => {
        server._router.stack = defaultRoute.slice();
    };

    server.listen(3000);

    return server;
};
github mjzhang1993 / react-router4-template / server / index.js View on Github external
const jsonServer = require('json-server');
const ip = require('ip').address();
const server = jsonServer.create();
const middlewares = jsonServer.defaults();
const createDB = require('./db.js');
const mounted = require('./route');
const DB = createDB();
const router = jsonServer.router(DB);

server.use(jsonServer.bodyParser);
server.use(middlewares);

mounted(server, DB);
server.use(router);

server.listen(
   {
      host: ip,
      port: 3167
github lechatquidanse / bicing-user-interface-app / docker / development / mock / server.js View on Github external
if (req.url === '/last-availabilities-by-station'
    || (req.url.includes('/stations') && false === req.url.includes('/stations/'))
    || req.url.includes('/stations-step-1')) {
    res.jsonp({
      'hydra:member': res.locals.data,
      '@context': 'local_bicing_http_call',
      '@id': 'local_bicing_http_call_id',
      '@type': 'local_bicing_http_call_type',
    })
  } else {
    res.jsonp(res.locals.data)
  }
}


const app = jsonServer.create();

let middlewaresOptions = {};

if (fs.existsSync()) {
  middlewaresOptions.static = path.join(__dirname, './public')
}
const middlewares = jsonServer.defaults(middlewaresOptions);

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, PATCH, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type');

  next();
});

json-server

[![Node.js CI](https://github.com/typicode/json-server/actions/workflows/node.js.yml/badge.svg)](https://github.com/typicode/json-server/actions/workflows/node.js.yml)

SEE LICENSE IN ./LICENSE
Latest version published 1 month ago

Package Health Score

88 / 100
Full package analysis