美文网首页
第十周学习笔记和总结

第十周学习笔记和总结

作者: 水水壶 | 来源:发表于2018-07-29 16:08 被阅读0次

这周主要接触了一下 Node。

一、 Node 服务器

const http = require('http');
const fs = require('fs');
const url = require('url');
const path = require('path');

const server = http.createServer(function (req, res) {
    if (req.url === '/favicon.ico') {
        return;
    }

    // 请求静态资源
    if (req.url !== '/') {
        let pathname = url.parse(req.url).pathname;
        const extname = path.extname(pathname);
        fs.readFile('.' + pathname, function (err, data) {
            if (err) {
                res.writeHead(404, {'Content-type': 'text/html;charset=UTF-8'});
                res.end('404: Resource Not Found.');
            }
            getmime(extname, function (mime) {
                res.writeHead(200, {'Content-type': mime});
                res.end(data);
            });
        });
    }

    // 首页
    if (req.url === '/') {
        res.writeHead(200, {'Content-type': 'text/html;charset=UTF-8'});
        res.write('<image src="/uploads/1.jpg" alt="照片"></image>');
        res.end();
    }

});

/*获取文件类型*/
function getmime (extname, callback) {
    fs.readFile('./config/mime.json', function (err, data) {
        if (err) {
            return console.log('未找到 mime.json 文件');
        }
        callback(JSON.parse(data)[extname]);
    })
}

server.listen(3000, function () {
    console.log('server is starting on port: 3000.');
});

node 作为 web 服务端,启动后会监听一个端口,一般是默认是port:80,在 web 客户端发送请求,Node 能够接受请求,并作出响应。

1. Node 服务器可以返回哪些资源

  • 可以返回 html 超文本 'Content-type': 'text/html;charset=UTF-8'
  • 可以返回纯文本 'Content-type': 'text/plain;charset=UTF-8'
  • 可以返回图片 'Content-type': 'image/jpeg'
  • 返回 json 格式 'Content-type': 'text/json'

如果 html 里有这三个标签,会分别再发一次请求。

  • <image src="1.jpg"></image>
  • <script src="index.js"></script>
  • <link href="style.css">

2.Node 服务器如何接受客户端传来的信息

  • params 接受参数,参数写在 url 里 /user/13
  • query(询问,用到问号) 参数也是写在 url 里,不过是写在问号后面 /user?name=dkvirus
  • post form 表单传值
  • 上传操作

二、 git 远程仓库

注册 github 帐号:https://github.com/dreamlin517

注册 gitee 帐号 : https://gitee.com/ydreamlin/events

相关文章

  • 第十周学习笔记和总结

    这周主要接触了一下 Node。 一、 Node 服务器 node 作为 web 服务端,启动后会监听一个端口,一般...

  • 十二周学习笔记和总结

    这周学习 express,写了一个网络相册,可以上传自己的图片并展示,代码放到了我的 github。 一、expr...

  • 十三周学习笔记和总结

    上周完成了相册上传和展示功能,这周完成了博客的展示、修改、新增和删除功能,我的代码放在 github 上。 一、C...

  • 学习笔记—周总结

    【优胜行动派️❤学习日记】 [打卡宝宝]:张雪 [打卡日期]:2019/2/17 [累计坚持]:这是我坚持学习的第...

  • 【学习笔记】周总结

    【优胜行动派️❤学习日记】 [打卡宝宝]:张雪 [打卡日期]:2019/1/27 [累计坚持]:这是我坚持学习的第...

  • 学习笔记~周总结

    [打卡宝宝]:张雪 [打卡日期]:2018/12/30 [累计坚持]:这是我坚持学习的第85天 [学习内容]:5分...

  • 第二周学习笔记和总结

    一、 display 和 float 这周具体遇到的都是将块级元素和行内元素转换,具体可以用 dispaly 和 ...

  • 第五周学习笔记和总结

    这周开始学习 js,主要做了下图这么一个效果,初步接触一些js的知识。 一、我写的代码 最终要实现的效果:点击其中...

  • 第九周学习笔记和总结

    首先了解程序运行的简单过程:一个程序会在电脑硬盘上长期储存,当启动程序时会把需要的文件放到内存上然后由内存传输给 ...

  • 第七周学习笔记和总结

    这周主要接触了 Bootstrap 、Node 和 npm、linux 常用命令,也尝试 用 Hexo 搭建了自己...

网友评论

      本文标题:第十周学习笔记和总结

      本文链接:https://www.haomeiwen.com/subject/fcgzmftx.html