打赏

node api demo

1.package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  "name": "node-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "multer": "^1.4.5-lts.1"
  }
}

  2.index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 导入模块
const express = require('express')
const path = require('path')
// 导入中间件
const cors = require('cors')
 
// 创建服务器
const app = express()
// 注册全局中间件
app.use(cors())
 
 
// 配置解析application x-www-form-urlencoded 模式的表单数据 的中间件
app.use(express.urlencoded({ extended: false}))
 
const host = 'http://127.0.0.1', port = 5001
 
// 启动服务器
app.listen(port,(req,res)=>{
    console.log(`app started at port ${ host }:${ port }`)
})
 
function sleep(ms) {
    return new Promise(resolve=>setTimeout(resolve, ms))
}
 
const router = express.Router()
 
// get
router.get('/test_get',(req,res)=>{
    res.send({
        code:200,
        data:"get"
    })
})
 
// get
router.get('/test_get2/:id',(req,res)=>{
    console.log(req.params)
    res.send({
        code:200,
        data:"get"
    })
})
 
// get
router.get('/test_get3',async (req,res)=>{
    await sleep(1000*3)
    res.send({
        code:200,
        data:"get"
    })
})
 
// post
router.post('/test_post',(req,res)=>{
    res.send({
        code:200,
        data:"post"
    })
})
 
// put
router.put('/test_put',(req,res)=>{
    res.send({
        code:200,
        data:"put"
    })
})
 
// delete
router.delete('/test_delete',(req,res)=>{
    res.send({
        code:200,
        data:"delete"
    })
})
// 文件下载
router.get('/download', (req, res) => {
    res.download('logo.png');
});
  
// 文件接收
const multer = require('multer')
 
 
let storage = multer.diskStorage({
    //设置文件存储路径
    destination: (req, file, cb) => {
        cb(null, "./upload"); // 相对于app.js文件路径
                 
    },
    //设置文件存储名称
    filename: (req, file, cb) => {
        let extname = path.extname(file.originalname); // 截取文件后缀名
        cb(null, `${file.fieldname}-${Date.now()}${extname}`);
        // 不需要我们执行IO操作,前端提交后,文件jian被存储到destination路径下,文件名为这里设置的文件名
    }
})
const upload = multer({storage})
 
// 2.存储 FormData 中名为 'upfile' 的文件
router.post('/upload', upload.single('upfile'), (req, res, next) => {
    // 3. 存储后的文件信息在 req.file 中,此时文件已经存储到本地了。
    console.log(req.file)
    res.send({
        code:200,
        data:"upload ok"
    })
})
 
app.use('/pyapi',router)

  

 

posted on   jlyuan  阅读(25)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

  • 随笔 - 52
  • 文章 - 1
  • 评论 - 1
  • 阅读 - 61911
点击右上角即可分享
微信分享提示