2
0

xhr_contacts_server.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const http = require('http');
  2. const port = 3000;
  3. // Back to the Future character data
  4. const characters = [
  5. {firstName: 'Marty', lastName: 'McFly', domain: 'mcfly.net'},
  6. {firstName: 'Emmett', lastName: 'Brown', domain: 'brown.com'},
  7. {firstName: 'Jennifer', lastName: 'Parker', domain: 'hillvalley.edu'},
  8. {firstName: 'Biff', lastName: 'Tannen', domain: 'tannen.org'},
  9. {firstName: 'George', lastName: 'McFly', domain: 'mcfly.net'},
  10. {firstName: 'Lorraine', lastName: 'Baines', domain: '1955.com'},
  11. {firstName: 'Strickland', lastName: 'Principal', domain: 'hillvalley.edu'},
  12. {firstName: 'Goldie', lastName: 'Wilson', domain: 'future.net'},
  13. {firstName: 'Clara', lastName: 'Clayton', domain: 'brown.com'},
  14. {firstName: 'Einstein', lastName: 'Dog', domain: 'brown.com'},
  15. {firstName: 'Dave', lastName: 'McFly', domain: 'mcfly.net'},
  16. {firstName: 'Linda', lastName: 'McFly', domain: 'mcfly.net'},
  17. {firstName: 'Marvin', lastName: 'Berry', domain: '1955.com'},
  18. {firstName: 'Lorraine', lastName: 'McFly', domain: 'mcfly.net'},
  19. {firstName: 'Match', lastName: 'Tannen', domain: 'tannen.org'},
  20. {firstName: 'Griff', lastName: 'Tannen', domain: 'tannen.org'},
  21. {firstName: 'Buford', lastName: 'Tannen', domain: 'tannen.org'},
  22. {firstName: 'Douglas', lastName: 'Needles', domain: 'hillvalley.edu'},
  23. {firstName: 'Terry', lastName: 'Needles', domain: 'hillvalley.edu'},
  24. {firstName: 'Betty', lastName: 'Needles', domain: 'hillvalley.edu'}
  25. ];
  26. const users = characters.map(char => ({
  27. jid: `${char.firstName.toLowerCase()}@${char.domain}`,
  28. fullname: `${char.firstName} ${char.lastName}`,
  29. chat_status: Math.round(Math.random())
  30. }));
  31. const server = http.createServer((req, res) => {
  32. const url = new URL(req.url, `http://${req.headers.host}`);
  33. const searchTerm = url.searchParams.get('q') || '';
  34. const filteredUsers = searchTerm
  35. ? users.filter(user =>
  36. user.fullname.toLowerCase().includes(searchTerm.toLowerCase()))
  37. : users;
  38. res.setHeader('Content-Type', 'application/json');
  39. res.setHeader('Access-Control-Allow-Origin', '*'); // Allow CORS
  40. res.end(JSON.stringify(filteredUsers, null, 2));
  41. });
  42. server.listen(port, () => {
  43. console.log(`Server running at http://localhost:${port}/`);
  44. });