-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakeapi.ts
39 lines (30 loc) · 982 Bytes
/
fakeapi.ts
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
39
/* global require, console */
import fs from "fs";
const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);
// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser);
server.use((req, _res, next) => {
if (req.method === "POST") {
req.body.createdAt = Date.now();
}
// Continue to JSON Server router
next();
});
server.post("/calc", (req, res) => {
console.log(req);
var data = JSON.parse(fs.readFileSync("data/example.json", "utf-8"));
// const response: BotOrNotResults = import("data/example.json");
res.jsonp(data);
});
// Use default router
server.use(router);
const port = 3001;
server.listen(port, () => {
console.log(`JSON Server is running on port ${port}`);
});