Json-server: integrate json-server with express server

Created on 4 Apr 2016  路  10Comments  路  Source: typicode/json-server

I'm trying to create a JSON-Server with express server where my entity models in JSON file.

any full example to help me with this?

// app.js file

var jsonServer = require('json-server');

// Returns an Express server
var server = jsonServer.create();

// Set default middlewares (logger, static, cors and no-cache)
server.use(jsonServer.defaults());

// Add custom routes
// server.get('/custom', function (req, res) { res.json({ msg: 'hello' }) })

// Returns an Express router
var router = jsonServer.router('db.json');

server.use(router);

server.listen(3000);

and running with

node app.js

Most helpful comment

Sure, here's how you can integrate it with Express:

var express = require('express');
var jsonServer = require('json-server');

var server = express();
// ...

// You may want to mount JSON Server on a specific end-point, for example /api
// Optiona,l except if you want to have JSON Server defaults
// server.use('/api', jsonServer.defaults()); 
server.use('/api', jsonServer.router('db.json'));

server.listen(3000);

Let me know if it helped.

All 10 comments

Sure, here's how you can integrate it with Express:

var express = require('express');
var jsonServer = require('json-server');

var server = express();
// ...

// You may want to mount JSON Server on a specific end-point, for example /api
// Optiona,l except if you want to have JSON Server defaults
// server.use('/api', jsonServer.defaults()); 
server.use('/api', jsonServer.router('db.json'));

server.listen(3000);

Let me know if it helped.

this is awesome thanks :+1:

very useful, thks! :+1:

But how about to add a custom router file for json-server (routes.json) in express app, not using express native router definition? I saw the routes file can be only use in CLI, did I miss anything?

Hello @mytharcher ,

I met the similar situation, have you already resolved the issue and if yes, could you pls kindly tell how to do?
Any help is highly appreciated!

Best regards, Jason

@xeoshow It has been a so long time and I totally forgot that. I think I didn't find a solution for that. And I hardly use json-server for mocking now. Because in most of our projects we are practicing full-stack developing, then we forget the troubles for mocking.

@mytharcher Thanks so much as well!

Hello @mytharcher ,
Sorry to bother you again, while I also found json-server is not that good for mocking, and you mentioned full-stack developing, could this resolve the mocking issue? What are they and how should do?
Any help is highly appreciated!

Best regards, Jason

That means each module in a system should be one's responsibility, from end to end (database tables/service/controller in backend, and stores/views/components in frontend). This workflow requires each developer in team to be real full-stack.

Then when you are developing frontend views, you need some data from backend API, you just make it work for your self. The mocking then will no longer be a issue.

If you what to see landing page from json-server

import express from 'express'
const app = express()

const db = genDB() // <- this is simple Object
app.use('/api', jsonServer.defaults(), jsonServer.router(db));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pantchox picture pantchox  路  3Comments

TXRRNT picture TXRRNT  路  4Comments

jasonlimantoro picture jasonlimantoro  路  4Comments

0plus1 picture 0plus1  路  3Comments

boydenhartog picture boydenhartog  路  3Comments