form.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/></head><body> <div class="container"> <form action="http://localhost:4000/doForm" enctype="multipart/form-data" method="POST" role="form"> <legend>添加小组成员</legend> <div class="form-group"> <label for="">名字</label> <input type="text" class="form-control" name="name" placeholder="请输入组员的名字"> </div> <div class="form-group"> <label for="">年龄</label> <input type="text" class="form-control" name="age" placeholder="请输入组员的年龄"> </div> <div class="form-group"> <label for="">上传头像</label> <input type="file" class="form-control" name="logo" > </div> <button type="submit" class="btn btn-primary">添加</button> </form> </div></body></html>
app.js
const koa =require("koa");
const koaBody = require("koa-body");
const app=new koa();
const router =require("koa-router")();
const fs=require("fs");
const path=require("path");
const static=require("koa-static")
const main =require('./models');
router.get("/find",async ctx=>{
var collection=await main();
var result=await collection.find().toArray();
ctx.body=result;
})
router.get("/add",async ctx=>{
var collection=await main();
var obj={
name:"dww",
age:17
}
var res =await collection.find({name:obj.name}).toArray();
if(res.length){
ctx.body="数据库中已添加了这位同学"
}else{
var result=await collection.insertOne(obj);
}
console.log(result);
})
router.post("/doForm",async ctx=>{
console.log(ctx.request.body);
var {name,age}=ctx.request.body;
age=Number(age);
var filename=ctx.request.files.logo.path
console.log(filename);
var reader=fs.createReadStream(filename);
var uploadFile=`./static/${path.basename(filename)}`;
var writer=fs.createWriteStream(uploadFile);
reader.pipe(writer)
var logo=ctx.origin+"/"+path.basename(filename);
const collection=await main();
await collection.insertOne({name,age,logo});
await ctx.redirect("/find")
})
app.use(koaBody({
multipart: true,
formidable: {
maxFileSize: 200 * 1024 * 1024,
keepExtensions: true
}
}))
app.use(async (ctx,next)=>{
ctx.set("Access-Control-Allow-Origin","*");
await next();
})
app.use(router.routes());
app.listen(4000);
modules-index.js
const {MongoClient}=require('mongodb');
const url='mongodb://123.60.82.25:12021';
const client=new MongoClient(url)
const dbName='student';
// async function main(table){
async function main(){
await client.connect();
console.log('Connected successfully to server');
const db =client.db(dbName);
// const collection =db.collection(table);
const collection =db.collection('username');
return collection;
}
module.exports=main;
package.json
{
"name": "11.16hand-form",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"art-template": "^4.13.2",
"koa": "^2.13.4",
"koa-art-template": "^1.1.1",
"koa-body": "^4.2.0",
"koa-router": "^10.1.1",
"koa-static": "^5.0.0",
"mongodb": "^4.1.4"
}
}