当前位置:网站首页>Testapi (node writes test interface without database, with basic data)
Testapi (node writes test interface without database, with basic data)
2022-07-21 04:47:00 【A good programmer】
The configuration required for the project is as follows
First step :node Project initialization
npm init --yes
nom i express
nom i cors
The second step : download npm Tripartite package express If you want to cross domain, you need to download cors
The overall project segment status is as follows
testApi-demo # Project directory
├── node_modules # Third party packages that the project depends on
└── routers # new express.Rouer()
└── jokeRouter.js # route
├── data # Imitate database ,
└── jokes.json # Joke data : What is stored is an array
├── package.json # Describe the project and the project
├── package-lock.json # Project package version locking and download address
├── app.js # node Entrance file
├── controller # obtain data Data in
└── jokelnfo #
The first part :get request ( Simple key value pairs )
First step : stay app.js Middle configuration
// I have not set up cross domain
const express = require('express')
//2. Create a server
const app = express()
// Import cors Allow cross-domain
//const cors = require('cors')
// Import joke route
const joke = require('./routers/jokeRouter.js')
// stay app.js Mount in
app.use('/joke',joke)
// Use cors Set allow cross domain
//app.use(cors())
//3. Turn on the server
app.listen(3000,()=>{
console.log('success')
})
Access address 172.0.0.1:3000/joke?num=1
The second step : stay routers/jokeRouter.js Middle configuration
// Import express
const express = require('express')
// Import the data ready to respond to the front end
const joke = require('../controller/jokeInfo.js')
const router = new express.Router()
router.get('/',joke)
module.exports = router
// Be careful :
// module.exports be used for node Module block development , Export a module , Corresponding import require(‘xxx modular ’)
// export default be used for ES6 Module block development , Export a module , Corresponding import import XXX from 'xxx modular ’
The third step : stay controller/jokelnfi.js The data transmitted to the front end is set in the controller
It is judged that the writing is not quite right : That's about it
// Import me in data Data in
const dataJokes = require('../data/jokes.json')
function joke(req,res){
// This is random data
// The length of the array 489, Send one randomly
// req.query Simple key value pairs get Request to get the data from the client
if(req.query.num){
function produceNum(n){
let arr=[]
let sum = []
for(let i = 1; i<=n;i++){
sum.push(dataJokes[parseInt(Math.random()*489)])
if(arr.indexOf([parseInt(Math.random()*489)])===-1){
arr.push([parseInt(Math.random()*489)])
}else{
i--
}
}
return sum
}
res.send({
status:0,
message:" Get user list data successfully ",
data:produceNum(req.query.num)
})
}else if(req.query instanceof Object){
res.send({
status:0,
message:" Get user list data successfully ",
data:dataJokes[parseInt(Math.random()*489)]
})
}
// console.log(dataJokes[parseInt(Math.random()*489)])
}
module.exports = joke
边栏推荐
- IP address forgery in security development
- DNA 7. 基因组拷贝数变异分析及可视化 (GISTIC2.0)
- Configuration of static routes to achieve network wide accessibility
- 编写一个Book类,该类至少有name和price两个属性。
- DOM节点类型
- Fun guessing game (not binary search!)
- Hibernate method to prevent SQL injection attack
- Solve tensoflow2 No module named:tensorflow contrib
- wangEditor Uncaught (in promise) Error: 初始化节点已存在编辑器实例,无法重复创建编辑
- 动态路由协议—OSPF综合实验
猜你喜欢
学会如何选择图表类型,小白也能玩转数据分析
DNA 7. Genome copy number variation analysis and visualization (gistic2.0)
HDLC PAP chap mGRE comprehensive experiment
DNA 10. 识别癌症驱动基因 (OncodriveCLUST)
QQ三方登录 - 前置环境和交互
LVM logical volume
SCS [1] today starts a single-cell journey, describing the past and present lives of single-cell sequencing
IF:4+ 铁代谢和免疫相关基因标记预测三阴性乳腺癌的临床结局和分子特征
Excel中万能的查询函数——VLOOKUP(使用方法+实操)
用C语言数组编写贪吃蛇
随机推荐
Yum local warehouse
Command line parameters of C language programming skills
掌握这些插件,分分钟提高你的办公效率90%!
wangEditor Uncaught (in promise) Error: 初始化节点已存在编辑器实例,无法重复创建编辑
MySQL数据库并发,上锁的问题(共享锁和排它锁)
JS笔试题--浏览器内核
Fun guessing game (not binary search!)
Database notes
Disk management and file system
基于STM32设计的动态密码锁
Operation skills of markdown in jupyter
面试题--事件循环
SCS【1】今天开启单细胞之旅,述说单细胞测序的前世今生
Ensp-nat comprehensive experiment
Three methods: arrange strings in reverse order (instead of printing in reverse order)
FigDraw 14. SCI 文章绘图之和弦图及文章复现(Chord Diagram)
IF:12+ 不同癌症中TMB与ICI反应之间的免疫相关因素研究
JS笔试题--随机数,数组去重
Comprehensive experiment of static routing
ES6新增重点