mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 06:34:02 +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()
|
||||
|
@ -1,8 +1,8 @@
|
||||
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) : {};
|
||||
function updateQueryString (target, sourceUrl) {
|
||||
return ~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {}
|
||||
}
|
||||
module.exports = (routes) => {
|
||||
const router = express.Router()
|
||||
@ -16,14 +16,14 @@ module.exports = (routes) => {
|
||||
target = target.replace(':' + param, req.params[param])
|
||||
}
|
||||
req.url = target
|
||||
req.query = updateQueryString(req.query,req.url)
|
||||
req.query = updateQueryString(req.query, req.url)
|
||||
next()
|
||||
})
|
||||
} else {
|
||||
router.all(route + '*', (req, res, next) => {
|
||||
// Rewrite url by replacing prefix
|
||||
req.url = req.url.replace(route, routes[route])
|
||||
req.query = updateQueryString(req.query,req.url)
|
||||
req.query = updateQueryString(req.query, req.url)
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user