-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a mock-server for api with file dbstorage through json server r…
…un with npm run mock
- Loading branch information
1 parent
6cfd713
commit 7e69410
Showing
10 changed files
with
2,164 additions
and
294 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug mock server", | ||
"cwd": "${workspaceFolder}/frontend", | ||
"program": "${workspaceFolder}/frontend/build/mock-server.js" | ||
}, | ||
] | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const jsonServer = require('json-server') | ||
const server = jsonServer.create() | ||
const router = jsonServer.router('static/cocktail.json') | ||
const middlewares = jsonServer.defaults() | ||
const SSE = require('express-sse') | ||
const status = { | ||
carrierDistMm: 0, | ||
payloadWeightGr: 0 | ||
} | ||
const sse = new SSE() | ||
|
||
server.get('/api/sse/status', sse.init) | ||
setInterval(() => { | ||
sse.send(status) | ||
status.carrierDistMm = Math.round(Math.random() * 500) | ||
status.payloadWeightGr += Math.round(Math.random() * 10) | ||
if (status.payloadWeightGr > 500) { | ||
status.payloadWeightGr = 0 | ||
} | ||
}, 1000) | ||
|
||
// Set default middlewares (logger, static, cors and no-cache) | ||
server.use(middlewares) | ||
server.use(jsonServer.bodyParser) | ||
|
||
// Add custom routes before JSON Server router | ||
server.post('/api/command/order', (req, res) => { | ||
console.log(req.body) | ||
if (Array.isArray(req.body) && | ||
req.body.every((i) => i.hasOwnProperty('ingredientId') && i.hasOwnProperty('ml'))) { | ||
// 409 if busy | ||
Math.random() > 0.5 ? res.send('ok') : res.sendStatus(409) | ||
} else { | ||
res.sendStatus(500) | ||
} | ||
}) | ||
|
||
server.post('/api/command/stop', (req, res) => { | ||
res.send('ok') | ||
}) | ||
|
||
server.get('/api/debug/move/:mm', (req, res) => { | ||
console.log(req.params) | ||
res.send(true) | ||
}) | ||
|
||
server.get('/api/debug/servo/:servoId/:aperture', (req, res) => { | ||
console.log(req.params) | ||
res.send(true) | ||
}) | ||
|
||
server.get('/api/debug/fill/:mm/:servoId/:aperture', (req, res) => { | ||
console.log(req.params) | ||
res.send(true) | ||
}) | ||
|
||
server.get('/api/debug/tare', (req, res) => { | ||
res.send('not implemented') | ||
}) | ||
|
||
server.get('/api/debug/test/:message', (req, res) => { | ||
res.send('ok') | ||
}) | ||
|
||
// Use default router | ||
server.use('/api', router) | ||
server.listen(3000, () => { | ||
console.log('JSON Server is running') | ||
}) |
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
Oops, something went wrong.