express接收get请求:
const express = require('express');
const app = express();
app.get('/index',(req,res) =>{
res.send(req.query)
})
app.listen(3000)

express接收post请求:
html
<!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 action="http://localhost:3000/add" method="post">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" name=""/>
</form>
</body>
</html>
js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
//拦截请求:
app.use(bodyParser.urlencoded({
extended:false
}))
app.post('/add',(req,res) =>{
res.send(req.body)
})
app.listen(3000)
静态文件的托管:
const express = require('express');
const path = require('path');
const app = express();
//需要托管那个静态文件css改为那个
app.use('/static',express.static(path.join(__dirname,"css")))
app.listen(3000)
网友评论