-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (30 loc) · 947 Bytes
/
server.js
File metadata and controls
38 lines (30 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// See https://github.com/typicode/json-server#module
const jsonServer = require("json-server");
const server = jsonServer.create();
// Uncomment to allow write operations
// const fs = require('fs')
// const path = require('path')
// const filePath = path.join('db.json')
// const data = fs.readFileSync(filePath, "utf-8");
// const db = JSON.parse(data);
// const router = jsonServer.router(db)
// Comment out to allow write operations
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
server.use(middlewares);
// Add this before server.use(router)
server.use(
jsonServer.rewriter({
"/api/*": "/$1",
"/blog/:resource/:id/show": "/:resource/:id",
})
);
server.use(router);
const JSON_SERVER_PORT = 3005;
server.listen(JSON_SERVER_PORT, () => {
console.log(
`JSON Server is running on: http://localhost:${JSON_SERVER_PORT}`
);
});
// Export the Server API
module.exports = server;