HTTP
code
var http = require('http');
http.createServer(function (request, response) {
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// 发送响应数据 "Hello World"
response.end('Hello World\n');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');
注解
引入 http 模块
var http = require('http');
创建 服务器回调函数 表示接收到请求后 的操作
http.createServer
response.end('Hello World\n'); 必须添加
listen
//监听端口
server.listen(3000,"127.0.0.1");
writeHead
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
text/plain 纯文本不渲染标签
//设置头部
res.writeHead(200,{"Content-Type":"text/html;charset=UTF8"});
write
res.write("<h2>我是2标题</h2>");