Last active
February 3, 2020 19:02
-
-
Save ribeiroevandro/12e4ccd5785d678cc3591738e28ff0ee to your computer and use it in GitHub Desktop.
Configuring json-server to expose static files, in addition to API endpoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"notifications": [ | |
{ | |
"id": 1, | |
"message": "Message", | |
"date": "2020-02-03T14:09:52.587Z" | |
}, | |
{ | |
"id": 2, | |
"message": "Message 2", | |
"date": "2020-02-03T14:09:52.587Z" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jsonServer = require('json-server'); | |
const server = jsonServer.create(); | |
const router = jsonServer.router('data.json'); | |
const middlewares = jsonServer.defaults({ static: 'src/assets' }); | |
const port = process.env.PORT || 3000; | |
server.use((req, res, next) => setTimeout(next, 500)); | |
server.use(middlewares); | |
server.use(router); | |
server.listen(port, function() { | |
console.log('JSON Server is running on http://localhost:' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment