首页 > 其他 > 详细

創建HTTP 服務器

时间:2014-09-26 00:43:59      阅读:317      评论:0      收藏:0      [点我收藏+]
 1  var http = require(‘http‘);
 2  var fs = require(‘fs‘);
 3  var server = http.createServer(function(req, res) {
 4      if (req.url !== "/favicon.ico") {
 5          req.on(‘data‘, function(data) {
 6              console.log(‘服務器端接受到的數據: ‘ + decodeURIComponent(data));
 7              res.write(‘服務器端接受到的數據: ‘ + decodeURIComponent(data));
 8          });
 9          res.writeHead(200, {
10              ‘Content-Type‘: ‘text/plain‘,
11              ‘Access-Control-Allow-Origin‘: ‘http://localhost‘
12          });
13          //這兩句等同於上面的res.writehead();
14          // res.setHeader(‘Content-Type‘: ‘text/plain‘);
15          // res.setHeader(‘Access-Control-Allow-Origin‘:‘http://localhost‘);
16          res.write(‘<html><head><meta charset="utf-8"/></head></html>‘);
17          res.write("服務器發來的數據");
18 
19      }
20      res.end();
21  }).listen(1337, "127.0.0.1");

 

創建HTTP 服務器

原文:http://www.cnblogs.com/xieyier/p/3993937.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!