nodeJs构建一个HttpServer

| |
[不指定 2016/09/04 20:19 | by 刘新修 ]

将下面代码放入server.js中

JavaScript代码
  1. var http = require("http");    
  2. http.createServer(function(request, response) {    
  3.     console.log('request received');    
  4.     response.writeHead(200, {"Content-Type": "text/plain"});    
  5.     response.write("Hello World");    
  6.     response.end();    
  7. }).listen(8888);    
  8. console.log('server started');    

执行node server.js
打开http://localhost:8888/,页面显示Hello World

JavaScript代码
  1. var http=require('http');  
  2.   
  3. var data={key:'value', hello:'world'};  
  4. var srv=http.createServer(function (req, res) {  
  5.   res.writeHead(200,{'Content-Type': 'application/json'});  
  6.   res.end(JSON.stringify(data));  
  7. });  
  8.   
  9. srv.listen(8080,function() {  
  10.   console.log('listening on localhost:8080');  
  11. });  

 

NodeJs平台 | 评论(0) | 引用(0) | 阅读(1357)