Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it doesn't work on xxx.js or remote url,though xxx.json is ok #1543

Open
nillwyc opened this issue Apr 29, 2024 · 1 comment
Open

it doesn't work on xxx.js or remote url,though xxx.json is ok #1543

nillwyc opened this issue Apr 29, 2024 · 1 comment

Comments

@nillwyc
Copy link

nillwyc commented Apr 29, 2024

it doesn't work on xxx.js or remote url,though xxx.json is ok

does anyone have the same problem as mine?

my Node.js:v18.19.0

this is a .js file in my work folder.

module.exports = () => {
  const data = { users: [] };
  // Create 1000 users
  for (let i = 0; i < 1000; i++) {
    data.users.push({ id: i, name: `user${i}` });
  }
  return data;
};

i tested these codes which copied on https://github.com/typicode/json-server/tree/v0
but something wrong

(base) PS T:\router> json-server --version
1.0.0-alpha.23

(base) PS T:\router> json-server http://jsonplaceholder.typicode.com/db
File http://jsonplaceholder.typicode.com/db not found

(base) PS T:\router> json-server --watch db.js
--watch/-w can be omitted, JSON Server 1+ watches for file changes by default
undefined:1
module.exports = () => {
^

SyntaxError: Unexpected token m in JSON at position 0
    at JSONFile.parse (<anonymous>)
    at JSONFile.read (file:///T:/myDev/env/nvm1.1.10/v18.19.0/node_modules/json-server/node_modules/lowdb/lib/adapters/node/DataFile.js:17:31)
    at async Observer.read (file:///T:/myDev/env/nvm1.1.10/v18.19.0/node_modules/json-server/lib/observer.js:21:22)
    at async Low.read (file:///T:/myDev/env/nvm1.1.10/v18.19.0/node_modules/json-server/node_modules/lowdb/lib/core/Low.js:16:22)
    at async file:///T:/myDev/env/nvm1.1.10/v18.19.0/node_modules/json-server/lib/bin.js:114:1

Node.js v18.19.0
@spanic
Copy link

spanic commented Oct 27, 2024

Well, .js files are not supported anymore as a source for database generation, as it comes from

json-server/src/bin.ts

Lines 128 to 136 in 6aa56d9

let adapter: Adapter<Data>
if (extname(file) === '.json5') {
adapter = new DataFile<Data>(file, {
parse: JSON5.parse,
stringify: JSON5.stringify,
})
} else {
adapter = new JSONFile<Data>(file)
}

Obviously, json-server expects only .json5 and .json files, these changes have been made in PR #1468. However you can install latest >= 0.X.X version (0.17.4 as for today), where .js still can be used:

} else if (is.JS(source)) {
// Clear cache
const filename = path.resolve(source)
delete require.cache[filename]
const dataFn = require(filename)
if (typeof dataFn !== 'function') {
throw new Error(
'The database is a JavaScript file but the export is not a function.',
)
}
// Run dataFn to generate data
const data = dataFn()
resolve(low(new Memory()).setState(data))

Looks like you also want to generate data - I also had the same task, and I solved it by creating.json file with Node.js script and then using this file as a source for json-server: https://gitverse.ru/austin_s/react-interview-task-v3/content/main/server/index.cjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants