http-test-server.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // import express from 'express';
  2. const express = require('express')
  3. import { Request, Response } from 'express';
  4. import { BaseMessage, LoggingService } from '../dependencies/logging/services/logging-service';
  5. import { LogSetting } from '../dependencies/logging/type/datatype';
  6. import { Subject } from 'rxjs';
  7. const app = express();
  8. const port = 9999;
  9. let count = 0
  10. let logSettings: LogSetting = {
  11. cacheMessageLimit: 0,
  12. storage: "MongoDB",
  13. setting: {
  14. appName: 'Retransmission',
  15. appLocName: 'Local Mongo Buffer',
  16. logLocName: 'Local Mongo BUffer',
  17. },
  18. customSetting: {
  19. server: "localhost:27017",
  20. database: 'client'
  21. }
  22. }
  23. let logginService = new LoggingService()
  24. let incomingMessage: Subject<BaseMessage> = new Subject()
  25. logginService.init(logSettings).then(() => {
  26. logginService.subscribe(incomingMessage)
  27. }).catch((err) => console.error(err))
  28. // Middleware to parse JSON bodies
  29. app.use(express.json());
  30. // Handling a GET request
  31. app.get('/', (req: Request, res: Response) => {
  32. res.send('Hello, I am alive!');
  33. });
  34. // Handling a POST request
  35. app.post('/data', (req: Request, res: Response) => {
  36. const data = req.body;
  37. count++
  38. console.log(`${count} : ${data?.header?.messageID ?? 'undefined Data'}`)
  39. incomingMessage.next(data)
  40. // Send a JSON response
  41. res.json({ message: `Received data`, data: data });
  42. // res.send(`Received data: ${data?.header?.messageID ?? 'undefined Data'}`);
  43. });
  44. app.listen(port, () => {
  45. console.log(`Server is running at http://localhost:${port}`);
  46. });