json-server
json-server copied to clipboard
why throw a error : The database is a JavaScript file but the export is not a function?
db.js
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 }
index.js
`const path = require('path')
const jsonServer = require('json-server')
const ip = require('ip').address()
const db = require('./db')
const server = jsonServer.create() const router = jsonServer.router(db()) const middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
server.listen({
host: "localhost",
port: 9090
}, function() {
console.log(JSON Server is running in http://${ip}:9090)
})
`
I have the same error.
//server.js var jsonServer = require('json-server') var server = jsonServer.create() var router = jsonServer.router('db.json') var middlewares = jsonServer.defaults() server.use(middlewares) server.listen(3000, () => { console.log('JSON Server is running') })
//db.json { "user": [ { "id": 1, "nombre": "asdasd", "apellido": "Siasdasdlva", "mail": "asdads" } ] }
//routes.json { "/api/*": "/$1", "/:resource/:id/show": "/:resource/:id", "/api/user": "/usuario/1" }
when i execute #>json-server server.js --routes routes.json
{^_^}/ hi!
Loading server.js C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\utils\load.js:28 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 (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\utils\load.js:28:13)
at start (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\run.js:122:5)
at module.exports (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\run.js:159:3)
at module.exports (C:\Users\Lautaro\AppData\Roaming\npm\node_modules\json-server\lib\cli\index.js:81:3)
at Object.
//================================================================//
if execute "node server.js --routes routes.json"
Basic server works but custom routes are ignored.
some idea?
facing the same issue. Any help?
Same issue is facing on my end. How can resolve this issue?
Same issue here.
same issue