Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

express body-parser 中间件 #5

Open
gaowei1012 opened this issue Jan 25, 2019 · 0 comments
Open

express body-parser 中间件 #5

gaowei1012 opened this issue Jan 25, 2019 · 0 comments

Comments

@gaowei1012
Copy link
Owner

gaowei1012 commented Jan 25, 2019

我们在编写node应用时候,获取前端表单传来的数据这是很常见的需求,我们现在以express为例, 创建服务器、但是我们后端要接受前端传来的数据,这是body-parser就派上了作用.

前端

const axios = require('axios')

// create server

axios({
	method: 'POST',
	url: 'http://localhost:8090/save',
	data: {
		username: '执念',
		password: '1243',
		email: 'zhinian@126.com'
	}
}).then(result => {
	console.log('请求发送成功',result)
})

后端

const express = require('express')
const bodyParser = require('body-parser')
const app = express()

// body-parser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

app.get('/', function (req, res, next) {
	res.send('express-body-parser 使用')
})

// save
app.post('/save', function(req, res, next) {
	let user = req.body;
	console.log(user)
})

app.listen(8090, function () {
	console.log('started 8090')
})

演示效果

qd

hd

到这里就是express body-parser的简单演示, 要想了解更多请查看官网

@gaowei1012 gaowei1012 changed the title nodejs body-parser 中间件 express body-parser 中间件 Jan 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant