express框架

express框架是nodejs的一个优秀的web框架,继承了许多简单易用的方法

1、创建web服务器并接受get请求

首先要安装express

npm install express --save

const express = require('express');

const app = express();

app.get('/getName', function(req, resp){
	resp.send('孙悟空');
});

app.listen('8000', function(){
	console.log('server is running at port 8000...');
});

2、post请求并获请求取参数

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

const app = express();
const urlencodedParser = bodyParser.urlencoded({ extended: false })

app.get('/getName', function(req, resp){
	resp.send('孙悟空');
});


app.post('/test_post', urlencodedParser, function (req, res) {
   var response = {
       "first_name":req.body.first_name,
       "last_name":req.body.last_name
   };
   console.log(response);
   res.end(JSON.stringify(response));
})


app.listen('8000', function(){
	console.log('server is running at port 8000...');
});
<html>
<body>
<form action="http://127.0.0.1:8000/test_post" method="POST">
First Name: <input type="text" name="first_name">  <br>
 
Last Name: <input type="text" name="last_name">
<input type="submit" value="Submit">
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值