Skip to content

Instantly share code, notes, and snippets.

@dearfrankg
Last active November 16, 2018 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dearfrankg/b8ca9bc4a9e55cb4dca56e7cee345346 to your computer and use it in GitHub Desktop.
Save dearfrankg/b8ca9bc4a9e55cb4dca56e7cee345346 to your computer and use it in GitHub Desktop.
intro-to-next-part2 -- mock data
...
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`;
...
{
"MOCK_URL": "http://localhost:3333/search/repositories"
}
"scripts": {
...
"mock": "node server"
},
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