mirror of
https://github.com/typicode/json-server.git
synced 2025-07-31 06:01:50 +08:00
Update
This commit is contained in:
@ -15,6 +15,7 @@
|
||||
"cors": "^2.3.0",
|
||||
"errorhandler": "^1.2.0",
|
||||
"express": "^4.9.5",
|
||||
"json-parse-helpfulerror": "^1.0.3",
|
||||
"lodash": "^4.11.2",
|
||||
"lowdb": "^0.14.0",
|
||||
"method-override": "^2.1.2",
|
||||
|
@ -1,5 +1,6 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const jph = require('json-parse-helpfulerror')
|
||||
const _ = require('lodash')
|
||||
const chalk = require('chalk')
|
||||
const enableDestroy = require('server-destroy')
|
||||
@ -189,25 +190,30 @@ module.exports = function (argv) {
|
||||
// Watch .js or .json file
|
||||
// Since lowdb uses atomic writing, directory is watched instead of file
|
||||
const watchedDir = path.dirname(source)
|
||||
let readError = false
|
||||
fs.watch(watchedDir, (event, file) => {
|
||||
<<<<<<< HEAD
|
||||
// https://github.com/typicode/json-server/issues/420
|
||||
// file can be null
|
||||
if (file) {
|
||||
const watchedFile = path.resolve(watchedDir, file)
|
||||
if (watchedFile === path.resolve(source)) {
|
||||
if (is.JSON(watchedFile)) {
|
||||
var obj
|
||||
let obj
|
||||
try {
|
||||
obj = JSON.parse(fs.readFileSync(watchedFile))
|
||||
obj = jph.parse(fs.readFileSync(watchedFile))
|
||||
if (readError) {
|
||||
console.log(chalk.green(` Read error has been fixed :)`))
|
||||
readError = false
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error reading JSON file');
|
||||
console.dir(e);
|
||||
return;
|
||||
readError = true
|
||||
console.log(chalk.red(` Error reading ${watchedFile}`))
|
||||
console.error(e.message)
|
||||
return
|
||||
}
|
||||
|
||||
// Compare .json file content with in memory database
|
||||
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
|
||||
const isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
|
||||
if (isDatabaseDifferent) {
|
||||
console.log(chalk.gray(` ${source} has changed, reloading...`))
|
||||
server && server.destroy()
|
||||
|
@ -2,7 +2,7 @@ const express = require('express')
|
||||
const url = require('url')
|
||||
const _ = require('lodash')
|
||||
function updateQueryString (target, sourceUrl) {
|
||||
return !!~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {};
|
||||
return ~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {}
|
||||
}
|
||||
module.exports = (routes) => {
|
||||
const router = express.Router()
|
||||
|
Reference in New Issue
Block a user