赞
踩
快速写出一个API接口来开发
const express = require('express')
- const app = express() //使用express函数
-
- app.use((request,response,next)=>{ //监听调用
- console.log('有人请求服务器1了');
- // console.log('请求来自于',request.get('Host'));
- // console.log('请求的地址',request.url);
- next()
- })
-
- app.get('/students',(request,response)=>{ //get的请求
- const students = [
- {id:'001',name:'tom',age:18},
- {id:'002',name:'jerry',age:19},
- {id:'003',name:'tony',age:120},
- ]
- response.send(students)
- })
-
-
-
- app.listen(5000,(err)=>{ //设置5000端口
- if(!err) console.log('服务器1启动成功了,请求学生信息地址为:
- http://localhost:5000/students'); //请求地址
- })
带参数:
- app.get('/student',(request,response)=>{
- const students = [
- {id:'001',name:'tom',age:18},
- {id:'002',name:'jerry',age:19},
- {id:'003',name:'tony',age:120},
- ]
- response.send(students)
- console.log(request.query); //带参数
- })
- const express = require('express')
- const app = express()
-
-
-
- var bodyParser=require('body-parser');
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({extended:true}));
-
- app.post('/form',(request,response)=>{
- response.send(request.body); //参数
- })
-
- app.listen(5000,(err)=>{
- if(!err) console.log('服务器1启动成功了,请求学生信息地址为:
- http://localhost:5000/from');
- })
nodejs
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。