express(body-parser插件获取数据)

  • npm install body-parser --save
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// 启动静态资源服务
app.use(express.static('public'));
// 处理表单提交的数据(默认表单的提交格式)
app.use(bodyParser.urlencoded({extended : false}));
// 处理 json 形式数据
app.use(bodyParser.json());
app.post('/login', (req, res) => {
    console.log(req.body);
    if (req.body.username == 'admin' && req.body.password == '123') {
        res.send('success post');
    }else{
        res.send('failure');
    }
});
app.get('/login', (req, res) => {
    console.log(req.query);
    if (req.query.username == 'admin' && req.query.password == '123') {
        res.send('success get');
    }else{
        res.send('failure');
    }
});
app.put('/login', (req, res) => {
    console.log(req.body);
    if (req.body.username == 'admin' && req.body.password == '123') {
        res.send('success put');
    }else{
        res.send('failure');
    }
});
app.delete('/login', (req, res) => {
    console.log(req.body);
    if (req.body.id == '1234') {
        res.send('success delete');
    }else{
        res.send('failure');
    }
})
app.listen(3000, () => {
    console.log('running……');
})

public/login.html



    
        
        登录页面
        
        
    
    
        
---提交地址和提交的方式 用户名:
密 码:
----用于正常的表单提交
-----用于ajax

你可能感兴趣的:(express(body-parser插件获取数据))