Node实现简单的接口及(文件)

tech2025-03-26  5

接口的定义

(1) 用户接口:操作系统专门为用户提供了“用户与操作系统的接口” ,通常称为用户接口。该接口支持用户与 OS 之间进行交互,即由用户向 OS 请求提供特定的服务,而系统则把服务的结果返回给用户。 (2)应用程序接口又称为应用编程接口,是一组定义、程序及协议的集合, 通过 API接口实现计算机软件之间的相互通信。API 的一个主要功能是提供通用功能集。API同时也是一种中间件,为各种不同平台提供数据共享。程序设计的实践中,编程接口的设计首先要使软件系统的职责得到合 理划分。 良好的接口设计可以降低系统各部分的相互依赖,提高组成单元的内聚性,降低组成单元间的耦合程度,从而提高系统的可维护性和可扩展性。

简而言之 最通俗的讲就你请求某一个api的接口。api响应你的请求要求给你反应响应相应的数据,就是所谓的交互,

文章目录

接口的定义一、简单接口(get例子)及返回json数据效果 二。简单(post例子)三。实例get带参数效果 四。实例post文件详情看注释


一、简单接口(get例子)

/** 接口:得到一条随机数据 接口地址: /joke 请求方式: get 参数:无 返回:一条笑话 **/ //导包 const express = require('express') //创建服务器 const app = express() //写接口 app.get('/joke', (req, res)=> { //准备n条笑话(实际开发的时候笑话们肯定是从数据库或者是其他的数据源获取到的.) let arr = ['狐狸走路容易摔跤,因为脚滑,哈哈','你好可爱',] // 开始创建一个随机的标 let index = Math.floor(Math.random()*3); // 返回数据 res.send(arr[index]); }) app.listen(6060,()=>{ console.log('服务开启') })

及返回json数据

const express = require('express') //创建服务器 const app = express() //写接口 app.get('/joke', (req, res)=> { //要 去设置一个响应头 res.setHeader( 'Content-Type', 'text/json'); ///设置响应回来的内容 res.send({ foodName:'随风倒十分', price:50, description: '沙发沙发' }) }) app.listen(6060,()=>{ console.log('服务开启') })

效果

二。简单(post例子)

验证一个密码

const express = require('express') //创建服务器 const app = express() const bodyParser = require('body-parser') app.use(bodyParser.urlencoded({ extended: false }))//转化格式 //写接口 app.post('/login', (req, res) => { //由于是post方式传递过来的参数,所以用req.query这种方式拿不到. // console. log(req); // console. log(req.query); //要想通过post传递过来的参数,就要使用第三方模块:body-parser console.log(req.body) //{user:'sa',pwd"123} //处理 if (req.body.username == 'admin' && req.body.password == ' 888888 ') { res.send({ code: 200, msg: '登录成功' }); } else { res.send({ code: 400, msg: '账号密码不对', }) } }); app.listen(8888, () => { console.log('服务开启') })

三。实例get带参数

/** /** *接口:查询英雄外号 根据英雄名返回英雄外号 *接口地址: /getNickName *请求方式: get 请求参数: heroName 英雄名(提莫/盖伦/李青...) *返回值:英雄外号 */ const express = require( 'express' ) const bodyParser = require('body-parser') const app = express() // 方式为x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended:false}))//转化格 //创建服务器 //写接口 app.get("/getname",(req,res)=>{ //要接收前端 传递过来的参数(英雄名) let heroNickName="" switch (req.query.heroName) {//根据请求头的数据传递回数据 case "提莫": heroNickName = "迅捷斥候"; break; case "李青": heroNickName = "盲僧"; break; case "盖伦": heroNickName = "德玛西亚之力"; break; case "亚索": heroNickName = "疾风剑豪"; break; case "阿狸": heroNickName ="九尾妖狐"; break; default: heroNickName = "该英雄不存在"; break ; } res.send(heroNickName) }) //开启服务器 app. listen(4399,()=>{ console. log("服务器开启了..."); })

效果

四。实例post文件

详情看注释

/** *注册接口 *接口地址: /register 本 请求方式: post *请求参数: username password usericon(用 户头像,图片文件)| */ const multer=require('multer') // 接受上传过来的文件放在哪个文件夹下 var upload=multer({dest:'pulic/'}) const express = require('express') //创建服务器 const app = express() //写接口 //需要第三方模块multer // 传过来的文件参数名指定为files.//如果是多给文件上传的话那就使用upload.array('参数','最大文件数') app.post('/register',upload.single('files'),(req,res)=>{ // 一起传过来的文件保存在req.body中 console.log(req.file)//打印传递过来的文件 // 但是传递过来的文件没有后缀 console.log(req.body) res.send('完成') }) app.listen(6060,()=>{ console.log('服务开启') })
最新回复(0)