test-login.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const http = require('http');
  2. // 测试发送验证码
  3. const sendCodeData = JSON.stringify({ phone: '13800138000' });
  4. const sendCodeOptions = {
  5. hostname: 'localhost',
  6. port: 3000,
  7. path: '/api/auth/send-code',
  8. method: 'POST',
  9. headers: {
  10. 'Content-Type': 'application/json',
  11. 'Content-Length': sendCodeData.length
  12. }
  13. };
  14. const req1 = http.request(sendCodeOptions, (res) => {
  15. let data = '';
  16. res.on('data', (chunk) => { data += chunk; });
  17. res.on('end', () => {
  18. console.log('发送验证码响应:', data);
  19. // 测试登录 (使用刚才获取的验证码 0227)
  20. const loginData = JSON.stringify({ phone: '13800138000', code: '0227' });
  21. const loginOptions = {
  22. hostname: 'localhost',
  23. port: 3000,
  24. path: '/api/auth/login',
  25. method: 'POST',
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. 'Content-Length': loginData.length
  29. }
  30. };
  31. const req2 = http.request(loginOptions, (res2) => {
  32. let loginResponse = '';
  33. res2.on('data', (chunk) => { loginResponse += chunk; });
  34. res2.on('end', () => {
  35. console.log('登录响应:', loginResponse);
  36. });
  37. });
  38. req2.on('error', (e) => console.error('登录请求错误:', e));
  39. req2.end();
  40. });
  41. });
  42. req1.on('error', (e) => console.error('发送验证码请求错误:', e));
  43. req1.write(sendCodeData);
  44. req1.end();