Last active
November 16, 2018 13:46
-
-
Save dearfrankg/b8ca9bc4a9e55cb4dca56e7cee345346 to your computer and use it in GitHub Desktop.
intro-to-next-part2 -- mock data
This file contains hidden or 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
... | |
import { MOCK_URL } from "../mock/config.json"; | |
const API_URL = MOCK_URL | |
? MOCK_URL | |
: `https://api.github.com/search/repositories?q=stars:>1+language:javascript&sort=stars&order=desc&type=Repositories`; | |
... |
This file contains hidden or 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
{ | |
"MOCK_URL": "http://localhost:3333/search/repositories" | |
} |
This file contains hidden or 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
"scripts": { | |
... | |
"mock": "node server" | |
}, |
This file contains hidden or 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("./mock/data.json"); | |
const middlewares = jsonServer.defaults(); | |
server.use(middlewares); | |
// rewrite routes | |
server.use( | |
jsonServer.rewriter({ | |
"/search/repositories": "/items", | |
"/search/repositories/:id": "/items/:id" | |
}) | |
); | |
// format outgoing data | |
router.render = (req, res) => { | |
res.jsonp({ | |
items: res.locals.data | |
}); | |
}; | |
server.use(router); | |
server.listen(3333, () => { | |
console.log("JSON Server is running"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment