22-Node.js学习笔记-Express-请求处理-Express路由参数

Express路由参数

``` //:id就是请求参数,占位符,意思是当前路由需要传递一个id app.get('/find/:id',(req,res)=>{ console.log(req.params);// {id:123} }); ``` ``` localhost:3000/find/123 ``` demo1 ``` //引入express框架 const express = require('express'); const bodyParser = require('body-parser'); //创建网站服务器 const app = express();

//拦截所有的请求

app.get('/index/:id',(req,res)=>{
//获取请求参数
res.send(req.params)
})

//监听端口
app.listen(3000);
console.log('网站服务器启动成功');

http://localhost:3000/index/7878
//{"id":"7878"}

demo2

//引入express框架
const express = require('express');
const bodyParser = require('body-parser');
//创建网站服务器
const app = express();

//拦截所有的请求

app.get('/index/:id/:name/:age',(req,res)=>{
//获取请求参数
res.send(req.params)
})

//监听端口
app.listen(3000);
console.log('网站服务器启动成功');

http://localhost:3000/index/7878/柠檬不酸/18
//{"id":"7878","name":"柠檬不酸","age":"18"}

原文地址:https://www.cnblogs.com/foreverLuckyStar/p/12089519.html