当前位置:网站首页>1-18 创建最基本的express服务器&创建路由的API模块
1-18 创建最基本的express服务器&创建路由的API模块
2022-06-30 21:26:00 【画不完的饼】
创建最基本的express服务器
router.js
创建get接口
//导入express
const express = require('express')
//创建路由对象
const apirouter = express.Router()
apirouter.get('/get',(req,res)=>{
const query = req.query
res.send({
status:0, //0表示处理成功,1表示处理失败
msg:'GET请求成功',//状态描述
data:query //需要响应给客户端的数据
})
})
apirouter.post('/post',(req,res)=>{
const body = req.body
res.send({
status:0, //0表示处理成功,1表示处理失败
msg:'POST请求成功',//状态描述
data:body //需要响应给客户端的数据
})
})
//向外导出路由对象
module.exports=apirouter
server.js
//创建基本的web服务器
const express =require('express')
const app = express()
//POST接口需要配置表单解析中间件来解析客户端传递过来的数据
app.use(express.urlencoded({extends:false}))
const router = require('./apirouter')
app.use('/api',router)
app.listen(80,()=>{
console.log('express server running at http://127.0.0.1')
})
边栏推荐
猜你喜欢

《ClickHouse原理解析与应用实践》读书笔记(1)

It is urgent for enterprises to protect API security

MySQL advanced 3

根据肠道微生物组重新思考健康饮食

Software engineering UML drawing

开源实习经验分享:openEuler软件包加固测试

多表操作-外键约束

Iclr'22 spotlight | how to measure the amount of information in neural network weights?

jenkins下载插件下载不了,解决办法

Icml2022 | utility theory of sequential decision making
随机推荐
Zaah Sultan looks at the old driver
【无标题】
Reading notes of Clickhouse principle analysis and Application Practice (1)
Test medal 1234
Phoenix architecture: an architect's perspective
毕业五年,想当初若没有入行测试,我是否还会如这般焦虑
双立体柱状图/双y轴
变异系数法matlab代码[通俗易懂]
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
k个一组反转链表
qsort函数和模拟实现qsort函数
根据肠道微生物组重新思考健康饮食
《ClickHouse原理解析与应用实践》读书笔记(1)
针对美国国家安全局“酸狐狸”漏洞攻击武器平台的分析与应对方案建议
凤凰架构——架构师的视角
CA I ah, how many times Oh, ah sentence IU home Oh 11111
ArcGIS构建发布简单路网Network数据服务及Rest调用测试
Text recognition svtr paper interpretation
兴奋神经递质——谷氨酸与大脑健康
Spatiotemporal data mining: an overview