From 0bac0352587ff58ac494b6390840f4309a75de82 Mon Sep 17 00:00:00 2001 From: typicode Date: Thu, 8 Dec 2016 23:29:43 +0100 Subject: [PATCH] Update --- package.json | 1 + src/cli/run.js | 22 ++++++++++++++-------- src/server/rewriter.js | 8 ++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index c6216df..d574c51 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/cli/run.js b/src/cli/run.js index fc78cfd..a67d730 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -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() diff --git a/src/server/rewriter.js b/src/server/rewriter.js index a48e910..e94ceb9 100644 --- a/src/server/rewriter.js +++ b/src/server/rewriter.js @@ -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() }) }