vue3.0以降のバックエンドインターフェイスサービス

vue3.0のts-nodeの使用

ダウンロードの準備をするための前提条件(TsNodeファイル内)

  • ダウンロード:
    • yarn add ts-node -g
  • 効果:
    • ts-nodeを使用してtsプロジェクトを直接実行する
  • package.jsonを初期化します
    • npm init -y
  • ノードのライフファイル
    • yarn add @types/node -D
  • エクスプレスミドルウェアをインストールする
    • yarn add express -S
  • ExpressLifeファイルをインストールする
    • yarn add @types/express -D
  • インストール:yarn add axios -S
  • インストール:yarn add typescript -g

バックグラウンドサービスindex.tsを開くだけです

import express,{
    
    Express,Router,Request,Response} from "express"
import axios from "axios"

const app:Express = express()
const router:Router = express.Router() // 分模块

app.use("/api",router) // 

router.get('/list',async (req:Request,res:Response)=>{
    
    
 const result = await axios.post('https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf')
 console.log("result",result);
 res.json({
    
    
   data:result.data 
 })
})

app.listen(9999,()=>{
    
    
  console.log("success server http://localhost:9999");
  
}) // 开一个服务 端口为3333

上記のものをインストールした後、スクリプトの説明を構成します

  • 「dev」:「ts-nodeindex.ts」、
  • ランタイム:yarn dev
{
    
    
  "dependencies": {
    
    
    "axios": "^0.26.1",
    "express": "^4.17.3",
    "ts-node": "^10.7.0",
    "typescript": "^4.6.2"
  },
  "name": "TsNode",
  "version": "1.0.0",
  "main": "index.js",
  "devDependencies": {
    
    
    "@types/express": "^4.17.13",
    "@types/node": "^17.0.21"
  },
  "scripts": {
    
    
    "dev": "ts-node index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

  • 実行後の効果
    • success server http://localhost:9999

バックエンドインターフェイスを確認してください

  • postmanを使用して確認する
    ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_43845137/article/details/123645711