This commit is contained in:
typicode
2016-12-08 23:29:43 +01:00
parent da7d61861c
commit 0bac035258
3 changed files with 19 additions and 12 deletions

View File

@ -15,6 +15,7 @@
"cors": "^2.3.0", "cors": "^2.3.0",
"errorhandler": "^1.2.0", "errorhandler": "^1.2.0",
"express": "^4.9.5", "express": "^4.9.5",
"json-parse-helpfulerror": "^1.0.3",
"lodash": "^4.11.2", "lodash": "^4.11.2",
"lowdb": "^0.14.0", "lowdb": "^0.14.0",
"method-override": "^2.1.2", "method-override": "^2.1.2",

View File

@ -1,5 +1,6 @@
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const jph = require('json-parse-helpfulerror')
const _ = require('lodash') const _ = require('lodash')
const chalk = require('chalk') const chalk = require('chalk')
const enableDestroy = require('server-destroy') const enableDestroy = require('server-destroy')
@ -189,25 +190,30 @@ module.exports = function (argv) {
// Watch .js or .json file // Watch .js or .json file
// Since lowdb uses atomic writing, directory is watched instead of file // Since lowdb uses atomic writing, directory is watched instead of file
const watchedDir = path.dirname(source) const watchedDir = path.dirname(source)
let readError = false
fs.watch(watchedDir, (event, file) => { fs.watch(watchedDir, (event, file) => {
<<<<<<< HEAD
// https://github.com/typicode/json-server/issues/420 // https://github.com/typicode/json-server/issues/420
// file can be null // file can be null
if (file) { if (file) {
const watchedFile = path.resolve(watchedDir, file) const watchedFile = path.resolve(watchedDir, file)
if (watchedFile === path.resolve(source)) { if (watchedFile === path.resolve(source)) {
if (is.JSON(watchedFile)) { if (is.JSON(watchedFile)) {
var obj let obj
try { 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) { } catch (e) {
console.log('Error reading JSON file'); readError = true
console.dir(e); console.log(chalk.red(` Error reading ${watchedFile}`))
return; console.error(e.message)
return
} }
// Compare .json file content with in memory database // Compare .json file content with in memory database
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState()) const isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
if (isDatabaseDifferent) { if (isDatabaseDifferent) {
console.log(chalk.gray(` ${source} has changed, reloading...`)) console.log(chalk.gray(` ${source} has changed, reloading...`))
server && server.destroy() server && server.destroy()

View File

@ -1,8 +1,8 @@
const express = require('express') const express = require('express')
const url = require('url') const url = require('url')
const _ = require('lodash') const _ = require('lodash')
function updateQueryString(target,sourceUrl) { 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) => { module.exports = (routes) => {
const router = express.Router() const router = express.Router()
@ -16,14 +16,14 @@ module.exports = (routes) => {
target = target.replace(':' + param, req.params[param]) target = target.replace(':' + param, req.params[param])
} }
req.url = target req.url = target
req.query = updateQueryString(req.query,req.url) req.query = updateQueryString(req.query, req.url)
next() next()
}) })
} else { } else {
router.all(route + '*', (req, res, next) => { router.all(route + '*', (req, res, next) => {
// Rewrite url by replacing prefix // Rewrite url by replacing prefix
req.url = req.url.replace(route, routes[route]) req.url = req.url.replace(route, routes[route])
req.query = updateQueryString(req.query,req.url) req.query = updateQueryString(req.query, req.url)
next() next()
}) })
} }