Skip to content

Commit 0bac035

Browse files
committed
Update
1 parent da7d618 commit 0bac035

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"cors": "^2.3.0",
1616
"errorhandler": "^1.2.0",
1717
"express": "^4.9.5",
18+
"json-parse-helpfulerror": "^1.0.3",
1819
"lodash": "^4.11.2",
1920
"lowdb": "^0.14.0",
2021
"method-override": "^2.1.2",

src/cli/run.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const jph = require('json-parse-helpfulerror')
34
const _ = require('lodash')
45
const chalk = require('chalk')
56
const enableDestroy = require('server-destroy')
@@ -189,25 +190,30 @@ module.exports = function (argv) {
189190
// Watch .js or .json file
190191
// Since lowdb uses atomic writing, directory is watched instead of file
191192
const watchedDir = path.dirname(source)
193+
let readError = false
192194
fs.watch(watchedDir, (event, file) => {
193-
<<<<<<< HEAD
194195
// https://github.com/typicode/json-server/issues/420
195196
// file can be null
196197
if (file) {
197198
const watchedFile = path.resolve(watchedDir, file)
198199
if (watchedFile === path.resolve(source)) {
199200
if (is.JSON(watchedFile)) {
200-
var obj
201+
let obj
201202
try {
202-
obj = JSON.parse(fs.readFileSync(watchedFile))
203+
obj = jph.parse(fs.readFileSync(watchedFile))
204+
if (readError) {
205+
console.log(chalk.green(` Read error has been fixed :)`))
206+
readError = false
207+
}
203208
} catch (e) {
204-
console.log('Error reading JSON file');
205-
console.dir(e);
206-
return;
209+
readError = true
210+
console.log(chalk.red(` Error reading ${watchedFile}`))
211+
console.error(e.message)
212+
return
207213
}
208-
214+
209215
// Compare .json file content with in memory database
210-
var isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
216+
const isDatabaseDifferent = !_.isEqual(obj, app.db.getState())
211217
if (isDatabaseDifferent) {
212218
console.log(chalk.gray(` ${source} has changed, reloading...`))
213219
server && server.destroy()

src/server/rewriter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const express = require('express')
22
const url = require('url')
33
const _ = require('lodash')
4-
function updateQueryString(target,sourceUrl) {
5-
return !!~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {};
4+
function updateQueryString (target, sourceUrl) {
5+
return ~sourceUrl.indexOf('?') ? _.assign(target, url.parse(sourceUrl, true).query) : {}
66
}
77
module.exports = (routes) => {
88
const router = express.Router()
@@ -16,14 +16,14 @@ module.exports = (routes) => {
1616
target = target.replace(':' + param, req.params[param])
1717
}
1818
req.url = target
19-
req.query = updateQueryString(req.query,req.url)
19+
req.query = updateQueryString(req.query, req.url)
2020
next()
2121
})
2222
} else {
2323
router.all(route + '*', (req, res, next) => {
2424
// Rewrite url by replacing prefix
2525
req.url = req.url.replace(route, routes[route])
26-
req.query = updateQueryString(req.query,req.url)
26+
req.query = updateQueryString(req.query, req.url)
2727
next()
2828
})
2929
}

0 commit comments

Comments
 (0)