为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
JavaScript

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

主讲老师: 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')
})

图片描述

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消