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");