zoukankan      html  css  js  c++  java
  • node.js----一个httpserver提交和解析get参数的例子

    • 前端代码
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="../../../../favicon.ico">
    
        <title>Sticky Footer Navbar Template for Bootstrap</title>
    
        <!-- Bootstrap core CSS -->
        <link href="../static/booststrap4/css/bootstrap.min.css" rel="stylesheet">
    
        <!-- Custom styles for this template -->
        <link href="../static/mycss/sticky-footer-navbar.css" rel="stylesheet">
    </head>
    
    <body>
    
    <header>
        <!-- Fixed navbar -->
        <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
            <a class="navbar-brand" href="#">Fixed navbar</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarCollapse">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Link</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link disabled" href="#">Disabled</a>
                    </li>
                </ul>
                <form class="form-inline mt-2 mt-md-0">
                    <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
                </form>
            </div>
        </nav>
    </header>
    
    <!-- Begin page content -->
    <main role="main" class="container">
        <h1 class="mt-5">Sticky footer with fixed navbar</h1>
        <p class="lead">Pin a fixed-height footer to the bottom of the viewport in desktop browsers with this custom HTML and CSS. A fixed navbar has been added with <code>padding-top: 60px;</code> on the <code>body &gt; .container</code>.</p>
        <p>Back to <a href="../sticky-footer">the default sticky footer</a> minus the navbar.</p>
        <form action="http://127.0.0.1:8090/?name=cpc&age=22" method="post">
    #这里action属性如此构造纯粹是上机实验的需要,为的是能使用fs或者http模块解析查询参数 <p> <textarea name="rec" cols="120" rows="8"> </textarea> </p> <p> <button type="submit" class="btn btn-outline-primary">提交</button> </p> </form> </main> <footer class="footer"> <div class="container"> <span class="text-muted">Place sticky footer content here.</span> </div> </footer> <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="../static/booststrap4/js/jquery-3.3.1.min.js"></script> </body> </html>

    效果:

    点击提交以后,后端js脚本能够解析get和post请求参数和数据

    • node.js后端代const http = require('http');var myquerystring = require('querystring');var myurl = require('url');var myfs = require('fs');var str="";
    http.createServer(function(req,res){
        var myreq = myurl.parse(req.url,true);
        console.log(myreq.query.name); #解析url中的参数name
        console.log(myreq.query.age); #解析url中的参数age    req.on('data',function (data) {
            str+=data;#由于提交过来的数据量是多次到达的,所以用了+=操作符
        });
        req.on('end',function () {
    #定义了提交完成后的动作
    const postdata = myquerystring.parse(str);#这里特地用querystring模块解析了提交的参数,也就是html里面name属性为rec的容器值 console.log(postdata); myfs.writeFile('the pulp fiction.txt',postdata.rec,function(err){
            #调用fs模块完成写入
    if (err) throw err; console.log('file saved'); }) }); res.end(); }).listen(8090);

    输出结果:

    cpc
    22
    [Object: null prototype] {
    rec:
    ' rec= Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 
    Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。 ' }
    file saved
    undefined
    undefined
    [Object: null prototype] {
    rec:
    ' rec= Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 
    Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。 ' }
    file saved
  • 相关阅读:
    直接报错了:无法加载文件 C:UsersAdministratorAppDataRoaming pmvue.ps1,因为在此系统中禁止执行脚本
    [vue系列]-vue+vue-i18n+elementUI 国际化
    new vue 实例发生了什么呢?
    vue引用外部JS的两种方案
    web轻量级富文本框编辑
    Cannot read property '_withTask' of undefined
    element 动态合并表格
    前端如何获取原始图片大小
    ASP.Net Core使用Ajax局部更新
    ASP.NET Core中的jQuery Unobtrusive Ajax帮助器
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/11025949.html
Copyright © 2011-2022 走看看