继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

【备战春招】第2天 破解JavaScript高级玩法 第九讲

ToRAmen
关注TA
已关注
手记 77
粉丝 10
获赞 5

课程章节: 深入探索网络请求

主讲老师: Cloud

课程内容:

今天学习的内容包括:

网络请求、HTTP详解

课程收获:

9.1 心得:

const express = require('express');
const path = require('path');
const multer = require('multer');

const server = express();

server.use(express.urlencoded({
    extended: true,
}));

server.use(express.json())

server.use(express.static(path.join(__dirname, './static')));


server.use("/urlencoded", function (req, res) {
    console.log("收到请求(urlencoded)");
    console.log("body:", req.body);
    res.json({
        code: 10000,
        data: req.body
    })
});

server.use("/multipart", multer().none(), function (req, res) {
    console.log("收到请求(multipart)");
    console.log("body:", req.body);
    res.json({
        code: 10000,
        data: req.body
    })
});


server.use("/json", multer().none(), function (req, res) {
    console.log("收到请求(json)");
    console.log("body:", req.body);
    res.json({
        code: 10000,
        data: req.body
    })
});




server.listen(3000, function () {
    console.log('listening at port 3000')
})

9.2 心得:

import http from "http";
import bodyParser from "body-parser";
import express from "express";
import createError from "http-errors";
// const multiparty = require('multiparty');

const port = 3000;

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
const server = http.createServer(app);

//设置跨域访问
app.use(function (req, res, next) {
    //设置允许跨域的域名,*代表允许任意域名跨域
    //"*"
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    //允许携带cookie
    res.header("Access-Control-Allow-Credentials", "true");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", [
        "X-PINGOTHER",
        "content-type",
        "Origin",
        "X-Requested-With",
    ]);
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");

    res.header("Access-Control-Max-Age", `${20}`);
    if (req.method.toLowerCase() == "options") res.send(200);
    //让options尝试请求快速结束
    else next();
});

app.post("/xhr", async (_req, _res) => {
    console.log("xhr: 收到请求");

    await sleep(10 * 1000);

    _res.json({
        code: 10000,
    });
});

function sleep(time: number) {
    return new Promise((resolve) => setTimeout(resolve, time));
}

app.get("/fetch", async (_req, res) => {
    console.log("fetch:收到请求", _req.url);
    await sleep(10 * 1000);
    return res.json({
        code: 10000
    });
});

app.get("/test1", (_req, res) => {
    res.send("test1");
});

app.get("/test2", (_req, res) => {
    res.send("test2");
});


app.get("/timeout", async (_req, res) => {
    await sleep(12 * 1000);
    res.send("test2");
});

app.get("/test4", async (_req, res) => {
    console.log("收到请求=test4=", _req.url);
    // res.send('hello')
    await sleep(30000);
    return res.json({
        REV: true,
        DATA: {
            msg: "成功",
        },
    });
});

server.listen(port, () => {
    console.log("监听端口:", port);
});

// catch 404 and forward to error handler
app.use(
    (
        _req: express.Request,
        _res: express.Response,
        next: express.NextFunction
    ) => {
        const error = createError(404);
        next(error);
    }
);

process.on(
    "unhandledRejection",
    (reason: {} | null | undefined, p: Promise<any>) => {
        console.error("自定义错误 Unhandled Rejection at:", p, "reason:", reason);
        // application specific logging, throwing an error, or other logic here
    }
);

9.3 心得:

const express = require('express');
const path = require('path');

const server = express();

server.use(express.urlencoded({
    extended: true,
}));
server.use(express.json())


function sleep(time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}

server.use(express.static(path.join(__dirname, './static')));

server.use("/1.css", async function (req, res) {
    await sleep(1000);
    res.sendFile(path.join(__dirname, './resources/1.css'))
});

server.use("/index.js", async function (req, res) {
    await sleep(3000);
    res.sendFile(path.join(__dirname, './resources/index.js'))
});
server.use("/index1.js", async function (req, res) {
    await sleep(8000);
    res.sendFile(path.join(__dirname, './resources/index1.js'))
});
server.use("/index2.js", async function (req, res) {
    await sleep(8000);
    res.sendFile(path.join(__dirname, './resources/index2.js'))
});
server.use("/index3.js", async function (req, res) {
    await sleep(8000);
    res.sendFile(path.join(__dirname, './resources/index3.js'))
});
server.use("/index4.js", async function (req, res) {
    await sleep(8000);
    res.sendFile(path.join(__dirname, './resources/index4.js'))
});
server.use("/index5.js", async function (req, res) {
    await sleep(8000);
    res.sendFile(path.join(__dirname, './resources/index5.js'))
});
server.use("/index6.js", async function (req, res) {
    await sleep(5000);
    res.sendFile(path.join(__dirname, './resources/index6.js'))
});


server.listen(3000, function () {
    console.log('listening at port 3000')
})

图片描述

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP