zoukankan      html  css  js  c++  java
  • 简单服务器的创建

    //用于创建网站服务器
    const http = require('http');
    //app对象就是网站服务器对象
    const app = http.createServer();
    //用于处理url地址
    const url = require('url');
    //当客户端有请求来的时候
    app.on('request', (req, res) => {
        //获取请求方式
        //req.method
        // console.log(req.method);
    
        //获取请求地址
        //req.url
        // console.log(req.url);
        
        //获取请求报文信息
        // console.log(req.headers['accept']);
        
        // http状态码: 500服务器端错误,404请求资源未被找到, 400客户端语法错误, 200请求成功
        //返回内容类型
        /*
        text/plain 纯文本
        text/html 
        text/css
        application/javascript
        image/jpeg
        application/json
         */
        res.writeHead(200, {
            'content-type': 'text/html;charset=utf8'
        });
    
        console.log(req.url);
        //参数1:要解析的url地址;参数2:将查询参数(query)解析为对象形式
        //即默认(默认为false和空)query的属性为字符串,而若第二个参数为true的话为对象
        // console.log(params.name);
        // console.log(params.age);
        // var pathname = url.parse(req.url,true).pathname;
        // console.log(pathname);
        let {query, pathname} = url.parse(req.url,true);
        if ( pathname === '/index' || pathname === '/') {
            res.end('<h2>欢迎来到首页</h2>');
        } else if (pathname === '/list') {
            res.end('welcome to listpage');
        } else {
            res.end('not find');
        }
    
        if ( req.method === 'POST') {
            res.end('post');
        } else if ( req.method === 'GET') {
            res.end('get');
        }
        // res.end('<h2>hello user</h2>');
    });
    //监听端口
    app.listen(3000);
    console.log('网站服务器启动成功');
  • 相关阅读:
    深度可分离卷积、分组卷积、空洞卷积、转置卷积
    Batch Normalization
    激活函数
    容器————vector
    39XML文档类
    38初识xml
    37QT程序打包
    36可视化操作数据库
    35使用模型操作数据库
    34sqlite
  • 原文地址:https://www.cnblogs.com/dokom666/p/12884300.html
Copyright © 2011-2022 走看看