node创建服务器 - Go语言中文社区

node创建服务器


//引入核心模块
const http = require('http');
//创建服务器
http.createServer((req,res)=>{
    
}).listen(3000);
//引入核心模块
const http = require("http");
//创建服务器
http.createServer((req,res)=>{
    console.log(req.url);
    console.log(req.method);
    //设置响应内容的格式
    //res.setHeader("Content-Type","text/plain;charset=utf8");
    //设置响应的状态码以及内容的格式
    res.writeHead(300,{"Content-Type":"text/html;charset=utf8"});
    //响应
    res.write("123");
    //最后的响应
    res.end("你好");
}).listen(9000);
//判断是否创建服务器成功
console.log("http://localhost:9000");
req:request   请求
res:response  响应
req.headers

ajax({
    type:"",//请求类型
    url:"",//请求路径
    data:{},//请求参数
    header:{//响应数据的类型
        content-type:"application/x-www-form-urllencoded";
    }
})

req.url  请求的路径
req.method  请求的方式 get  post
req.header

res 的方法:
    res.statusCode()  设置状态码
    res.write()  响应
    res.end()  最后的响应
        write:write  可以多次
        end:write+end  只能一次
    res.setHeader()  设置响应内容的格式
        第一个值是 content-type
        第二个值是内容格式
            text/plain  文本
            text/html  html文件
            text/css   css文件
            application/x-javascript  js文件
            applocation/json   json文件
        res.writeHead()  设置响应的状态码以及响应内容的格式  其实这个方法是statusCode 与setHeader的综合写法
        参数1:状态码
        参数2:对象  key   :  value
              Content-type:响应内容的格式

 

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/dongwei1/p/10519893.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-17 14:24:33
  • 阅读 ( 939 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢