zoukankan      html  css  js  c++  java
  • 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:响应内容的格式
  • 相关阅读:
    Django安装和启动
    转载:Python 包管理工具解惑
    电子商务的基本理念
    javascript的循环使用
    错误码
    weex初始
    Flex 布局
    css样式重置表
    手机端页面自适应解决方案—rem布局
    实用的60个CSS代码片段[下]
  • 原文地址:https://www.cnblogs.com/dongwei1/p/10519893.html
Copyright © 2011-2022 走看看