json-server icon indicating copy to clipboard operation
json-server copied to clipboard

How to set defaults options as module?

Open lixiaofeng666 opened this issue 10 years ago • 3 comments

var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()
server.use(middlewares)

I try to set static directory,it did not work. :(

var middlewares = jsonServer.defaults({static:'./app'})

lixiaofeng666 avatar May 17 '16 05:05 lixiaofeng666

@PhoonLee were you able to figure out how to log the static files?.

pratikgala avatar Oct 20 '17 19:10 pratikgala

I wanted to use something like this var jsonServer = require('json-server') var server = jsonServer.create() var router = jsonServer.router('db.json') var middlewares = jsonServer.defaults({quiet: true}); server.use(middlewares)

it is also not working, I want to stop logs on the output window

piku0709 avatar Jul 29 '20 19:07 piku0709

Sorry for 4 years late response :)

Based on the documentation:

The path you provide to the jsonServer.router function is relative to the directory from where you launch your node process. If you run the above code from another directory, it’s better to use an absolute path:

const path = require('path')
const router = jsonServer.router(path.join(__dirname, 'db.json'))

so try:

const path = require('path');
const router = jsonServer.defaults({
  static: path.join(__dirname, './app')
});

andriy101 avatar Sep 24 '20 22:09 andriy101