app.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const express = require("express");
  2. const res = require("express/lib/response");
  3. const http = require("http");
  4. const app = express();
  5. const port = 3000;
  6. //TODO load these values from setting or env
  7. const irohaApiHost = "localhost";
  8. const irohaApiPort = 5000;
  9. const irohaDomain = "test";
  10. app.post("/price", (req, res) => {
  11. //TODO implement price calculation depending on extent area
  12. res.send({
  13. price: 33
  14. });
  15. });
  16. app.get("/users/:userId/assets", (req, res) => {
  17. let output = '';
  18. let options = {
  19. hostname: irohaApiHost,
  20. port: irohaApiPort,
  21. path: '/accounts/' + req.params.userId + '@' + irohaDomain + '/assets/',
  22. method: 'GET',
  23. }
  24. let irohaReq = http.request(options, irohaRes => {
  25. irohaRes.on("data", chunk => {
  26. output += chunk;
  27. });
  28. irohaRes.on('end', () => {
  29. res.send(JSON.parse(output));
  30. })
  31. });
  32. irohaReq.on("error", error => {
  33. console.error(error);
  34. res.send(error);
  35. });
  36. irohaReq.end();
  37. });
  38. app.get("/users/:userId/assets/:assetId", (req, res) => {
  39. //TODO implement...
  40. res.send({
  41. assetId: req.params.assetId,
  42. balance: 150
  43. });
  44. });
  45. app.post("/buy", (req, res) => {
  46. //TODO implement...
  47. res.status(201);
  48. });
  49. app.listen(port, () => {
  50. console.log(`Listening at http://localhost:${port}`)
  51. })