Giter VIP home page Giter VIP logo

route's Introduction

route

node模仿express封装路由

var http = require('http');
var url = require('url');

// node模仿express封装路由
var R = {}; //R用来接收参数
// R._get = {}, R._post = {},
methods = ["get", "post"];
var app = (req, res) => {
    var pathname = url.parse(req.url).pathname;
    if (!pathname.endsWith("/")) pathname += "/";
    var method = req.method.toLowerCase();
    // R["_"+method][pathname] ? R["_"+method][pathname](req, res) : res.end("网址不存在");
    if (R["_" + method][pathname]) {
        if (method === "get") {
            R["_" + method][pathname](req, res)
        } else if (method === "post") {
            var data = "";
            req.on("data", (chunk) => {
                data += chunk;
            })
            req.on("end", () => {
                res.body = data;
                R["_" + method][pathname](req, res);
            })
        }
    } else {
        res.end("Not Found");
    }
}
methods.forEach((v) => {
    R["_" + v] = {};
    app[v] = (string, fun) => {
        if (!string.startsWith("/")) string = "/" + string;
        if (!string.endsWith("/")) string += "/";
        R["_" + v][string] = fun
    }
})
http.createServer(app).listen(9001);    // 把app提出去
app.get("/", (req, res) => {
    res.writeHead(200, { "Content-Type": "text/html;charset='utf-8'" });
    res.end(`
        <form action='/form' method='POST'>
            <input type='text' name='text'>
            <input type='submit'>  
        </form>
    `)
})
app.post("/form", (req, res) => {
    res.end(res.body)
})

route's People

Contributors

nocodeempire avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.