server2.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var http = require('http');
  2. var fs = require('fs');
  3. var con = require('./conn.js');
  4. global.data1 = con.e;
  5. var server = http.createServer();
  6. server.listen(8082, function(){
  7. console.log('服务器正在端口8082上运行......');
  8. })
  9. server.on('request',function(request,response){
  10. var url = request.url;
  11. if(url ==='/'){
  12. //response.writeHead(响应状态码,响应头对象): 发送一个响应头给请求。
  13. response.writeHead(200,{'Content-Type':'text/html'})
  14. // 如果url=‘/' ,读取指定文件下的html文件,渲染到页面。
  15. fs.readFile('./practice/login.html','utf-8',function(err,data){
  16. if(err){
  17. throw err ;
  18. }
  19. response.end(data);
  20. });
  21. }else if(url === '/login'){
  22. response.writeHead(200,{'Content-Type':'text/html'});
  23. // 如果url=‘/' ,读取指定文件下的html文件,渲染到页面。
  24. fs.readFile('./practice/login.html','utf-8',function(err,data){
  25. if(err){
  26. throw err ;
  27. }
  28. response.end(data);
  29. });
  30. }else if(url === '/index'){
  31. response.writeHead(200,{'Content-Type':'text/html'});
  32. // 如果url=‘/' ,读取指定文件下的html文件,渲染到页面。
  33. var con = require('./conn.js');
  34. fs.readFile('./practice/index2.html','utf-8',function(err,data){
  35. if(err){
  36. throw err ;
  37. }
  38. response.end(data);
  39. });
  40. /*
  41. var con = require('./conn.js');
  42. var s = '';
  43. for(var i = 0; i < con.e.length; i++)
  44. {
  45. s+='<tr>'
  46. +'<td>' + con.e[i]['userid'] + '</td>'
  47. +'<td>' + con.e[i]['ip'] + '</td>'
  48. +'<td>' + con.e[i]['monitorTime'] + '</td>'
  49. +'<td>' + con.e[i]['jyjid'] + '</td>'
  50. +'<td>' + con.e[i]['jyqid'] + '</td>'
  51. +'<td>' + con.e[i]['al'] + '</td>'
  52. +'<td>' + con.e[i]['qll'] + '</td>'
  53. +'<td>' + con.e[i]['yll'] + '</td>'
  54. +'</tr>'
  55. }
  56. var t = '<table border="1">'
  57. +' <tr>'
  58. +'<th>用户标识</th>'
  59. +'<th>ip</th>'
  60. +'<th>时间</th>'
  61. +'<th>油机</th>'
  62. +'<th>油枪</th>'
  63. +'<th>气液比</th>'
  64. +'<th>气流量</th>'
  65. +'<th>油流量</th>'
  66. +'</tr>'
  67. +s
  68. +'</table>';
  69. var html = '<html>'
  70. +'<head>'
  71. +'<title>nodejs</title>'
  72. +'<meta charset="utf-8"> '
  73. +'</head>'
  74. +'<body>'
  75. //+ s
  76. + t
  77. +'</body>'
  78. response.end(html);
  79. */
  80. }else{
  81. response.writeHead(200,{'Content-Type':'text/html'});
  82. // 如果url=‘/' ,读取指定文件下的html文件,渲染到页面。
  83. fs.readFile('./practice/'+request.url,'utf-8',function(err,data){
  84. if(err){
  85. throw err ;
  86. }
  87. response.end(data);
  88. });
  89. }
  90. });