Using the simple example of db.json containing
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
} and then making server.js containing as per the example in module
var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
server.listen(3000, function () {
console.log('JSON Server is running')
})
and then running json-server server.js
the error is shown
\{^_^}/ hi!
Loading server.js
/usr/local/lib/node_modules/json-server/src/cli/utils/load.js:22
throw new Error('The database is a JavaScript file but the export is not a function.')
^
Error: The database is a JavaScript file but the export is not a function.
at module.exports (/usr/local/lib/node_modules/json-server/src/cli/utils/load.js:22:13)
at start (/usr/local/lib/node_modules/json-server/src/cli/run.js:99:5)
at module.exports (/usr/local/lib/node_modules/json-server/src/cli/run.js:126:3)
at module.exports (/usr/local/lib/node_modules/json-server/src/cli/index.js:86:3)
at Object.<anonymous> (/usr/local/lib/node_modules/json-server/bin/index.js:2:22)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
If I am doing something wrong, perhaps the documentation is not clear enough...
Hi @Isanderthul,
Thank you for reporting this issue. Simply start your server using the following command-line:
node server.js
Or even better using nodemon to automatically restart server on change.
I'll update the README to make it clearer how to start the project.
Thank you!
is there any way to start json-server with node server.js and also apply CLI options such as delay?
Most helpful comment
is there any way to start json-server with
node server.jsand also apply CLI options such asdelay?