【node学习】koa2实现简单的表单提交 入门前端+后台完整代码

news/2024/7/24 9:31:54 标签: node, koa2, 表单提交

开始一个小系列的博客,记录自己的学习过程。koa2实现简单的登录页面的表单提交。入门级别,没有数据库操作,开始接触koa2小接口的编写。

推荐一个不错的koa2的基础教程:https://blog.csdn.net/lszy16/article/list/2 

前端请求到数据的效果图:

后端代码实现:

const Koa=require('koa')
const router=require('koa-router')()        //路由插件
const fs=require('fs')                      //文件系统  读取/写入文件

const app =new Koa()
const bodyParser=require('koa-bodyparser')     //获取post数据
app.use(bodyParser())

router.get('/user',async(ctx,next)=>{
    ctx.type='html'
    ctx.body=fs.createReadStream('./static/2.html')    //读取静态文件。。此处为前端文件
})

router.post('/user/regiser',async(ctx,next)=>{         //表单提交接口
    console.log(ctx.request.body)
    let {name,password}=ctx.request.body
    ctx.response.set("Content-Type", "application/json")
    if(name==='aaa'&&password==='123456'){
        
        ctx.status = 200;
        ctx.body = {                                   //post返回接口数据
            status_code:'0',
            data: 'register success'
        };
    }else{
        ctx.body = {
            status_code:'1001',
            data: '账号或密码错误'
        };
    }
})

app.use(router.routes())

app.listen(3000,()=>{
    console.log('server')
})

前端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form id="form" action="##" method="post" οnsubmit="return false"  enctype:'multipart/form-data'>
        <input type="text" name="name">
        <input type="password" name="password">
        <button class="btn">go</button>
    </form>
</body>
</html>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(".btn").click(function(){
    console.log(4444)
    $.ajax({
        url:'/user/regiser',
        type:"post",
        data:$("#form").serialize(),
        success:function(res){
            console.log(res)
        }
    })
})
</script>





http://www.niftyadmin.cn/n/1202939.html

相关文章

redsi一主两从三哨兵

1.前提准备 防火墙,selinux,主机名解析&#xff0c;所有主机安装gcc [rootlocalhost ~]# vim /etc/hosts 192.168.122.135 redis_master 192.168.122.136 redis_slave01 192.168.122.137 redis_slave02 192.168.122.138 sentinel01 192.168.122.139 sentinel02 192.168.12…

layui表格隐藏列的简单实现方法

需求&#xff1a;渲染表格的时候&#xff0c;比如id在内的一些列是不需要展示给用户看的&#xff0c;但是在操作表格的时候还需要用得到&#xff0c;这就需要隐藏列 的功能 实现效果&#xff1a;页面上不显示&#xff0c;但是能获取到id的值。 实现方法&#xff1a; table.ren…

春晚红包:挺住的百度和崩坏的应用商店

2019年2月5日&#xff0c;当中央电视台春节联欢晚会落下帷幕的时候&#xff0c;笔者的朋友圈出现了这样一条消息&#xff1a;“顺利抗住宇宙第一大IP&#xff0c;全程无宕机&#xff01;” 挺住的百度 相信很多人都猜到了上面这条朋友圈是出自百度同学之手。这个事情要从2019年…

layui服务器端分页实例

layui的分页功能是很不错的&#xff0c;方便快捷。jquery普通页面开发可以考虑。下面以表格为例&#xff0c;举例layui分页功能。其他&#xff08;利于list等&#xff09;同理。首先将layui.css、layui.all.js引入在页面中。 官网&#xff08;下载地址&#xff09;&#xff1a…

牛客寒假6-A.出题

链接&#xff1a;https://ac.nowcoder.com/acm/contest/332/A 题意&#xff1a; 小B准备出模拟赛。 她把题目按难度分为四等&#xff0c;分值分别为6,7,8,9。 已知小B共出了m道题&#xff0c;共n分。 求小B最少出了多少道6分题。 思路&#xff1a; n无解的条件&#xff1a;n &…

【echarts应用】---pie饼图篇

目的&#xff1a;对echart 图表进行封装调用--饼图篇。下面是基础的样式&#xff0c;可以改成自己需要的样式。&#xff08;找个地方保存&#xff0c;以后用到的时候方便复制粘贴&#xff0c;哈哈&#xff09; 1.页面中引入echarts、jquery文件 <script src"./js/jque…

【echats应用】---gauge仪表盘篇

目的&#xff1a;对echarts图表进行封装调用--仪表盘篇。 基础的仪表盘有两种方式都可以实现&#xff0c;一是echarts图表&#xff0c;一是css3的方式也可以实现。主要讲解echarts的方式 法一&#xff1a;echarts方法 普通样式 …

【node学习】koa2搭建简单的服务器,读取json文件打开图表项目

需求&#xff1a;用koa2搭建一个简单的服务器&#xff0c;能够读取json文件打开echarts图表项目。我们知道&#xff0c;不能直接打开图表文件&#xff0c;可以使用hbuilder这种自带内置服务器的编辑器&#xff0c;或者vscode的live-server插件打开。如果不想在电脑上下载很多编…