---
title: "Express Get和Post获取参数"
author: "fairy_xzc"
date: "2017-01-22T20:55:17Z"
tags: []
categories: ["Web前端"]
slug: "express-get-post"
draft: false
lastmod: "2017-01-22T20:57:33Z"
id: 2354503
url: "https://agilestyle.iteye.com/blog/2354503"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2354503](/blog/2354503)**

 

## Project Directory

![](http://dl2.iteye.com/upload/attachment/0122/7531/bc4eda6b-7008-395a-90b2-6dff11921d98.png)

 

### npm install express

![](http://dl2.iteye.com/upload/attachment/0122/7533/ff446423-8edf-3540-a67b-3f249e274e8c.png)  
 

### npm install body-parser

![](http://dl2.iteye.com/upload/attachment/0122/7535/6b0b9f09-a554-3ffb-a8d9-cd6ced8d873a.png)Note：post提交需要这个body-parser来获取参数 

 

### index.html

```
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>express get & post</title>
</head>
<body>
	<form action="/testget" method="get">
		<label for="username">username:</label><input type="text" name="username"><br>
		<label for="password">password:</label><input type="password" name="password"><br>
		<input type="submit" value="test-get">
	</form>

	<hr>

	<form action="/testpost" method="post">
		<label for="username">username:</label><input type="text" name="username"><br>
		<label for="password">password:</label><input type="password" name="password"><br>
		<input type="submit" value="test-post">
	</form>
</body>
</html>
```

 

### index.js

```
var express = require('express');
var bodyParser = require('body-parser');
var app = express();

var urlencodedParser = bodyParser.urlencoded({
	extended: false
});

app.use(bodyParser.urlencoded({
	extended: false
}));

app.get('/', function(req, res) {
	res.sendFile(__dirname + "/index.html");
});

app.get('/testget', function(req, res) {
	// http://localhost:8888/testget?username=hello&password=world
	var username = req.query.username;
	var password = req.query.password;

	console.log(username);
	console.log(password);

	res.send('Hello Get');
});

app.post('/testpost', function(req, res) {
	// http://localhost:8888/testpost
	var username = req.body.username;
	var password = req.body.password;

	console.log(username);
	console.log(password);

	res.send('Hello Post');
});

app.listen(8888, function() {
	console.log('listening on port 8888');
});
```

 

## Run

**test-get，username:hello, password:world**

![](http://dl2.iteye.com/upload/attachment/0122/7537/881489f1-0340-3e11-9424-6da5d7c8a30c.png)

查看Browser

![](http://dl2.iteye.com/upload/attachment/0122/7539/f4f6dc09-3bfa-39d0-96e1-4e73f15515a8.png)  
 

查看Console

![](http://dl2.iteye.com/upload/attachment/0122/7541/7a2490ec-b44b-3fe0-8d18-24e5f53e9f1f.png)  
 

**test-post，username:sky, password:funny**

![](http://dl2.iteye.com/upload/attachment/0122/7543/c950001b-d723-3e74-8230-319f4be7361d.png)

查看Browser 

![](http://dl2.iteye.com/upload/attachment/0122/7545/5b19f776-b802-324b-9769-58cecb993d6f.png)  
查看Console

![](http://dl2.iteye.com/upload/attachment/0122/7547/dd3e2188-57bd-3752-9e5e-48f390ae39b9.png)