当前位置: 移动技术网 > IT编程>开发语言>JavaScript > node创建服务器

node创建服务器

2019年03月13日  | 移动技术网IT编程  | 我要评论
//引入核心模块
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:响应内容的格式

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网