-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyserver.js
35 lines (30 loc) · 929 Bytes
/
myserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
app.use(async(ctx)=>{
if(ctx.url==='/' && ctx.method==='GET'){
//显示表单页面
let html=`
<h1>JSPang Koa2 request POST</h1>
<form method="POST" action="/">
<p>userName</p>
<input name="userName" /><br/>
<p>age</p>
<input name="age" /><br/>
<p>website</p>
<input name="webSite" /><br/>
<button type="submit">submit</button>
</form>
`;
ctx.body=html;
}else if(ctx.url==='/' && ctx.method==='POST'){
let postData= ctx.request.body;
ctx.body=postData;
}else{
ctx.body='<h1>404!</h1>';
}
});
app.listen(3000,()=>{
console.log('[demo] server is starting at port 3000');
});