Nodejs+Express 权限下载文件

先请求一个TOKEN(以用户生成,并且设置过期时间). 以token做验证去下载.

或者直接发送登录的token下载


const express = require('express');
const app = express();
const port = 51413;
const bodyParser = require('body-parser');
app.use(require('cors')())
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(bodyParser.json())
app.use(express.static('./public'))

const jwt = require('jsonwebtoken')

app.get('/getUrl', (req, res) => {
    let token = jwt.sign({
        tmp: Date.now()
    }, '123', { expiresIn: 6000 })

    res.json({
        token
    })

})
app.get('/dowFile/:token',(req,res)=>{
    let token = req.params.token;
    try {
        userInfo = jwt.verify(token, '123');
        console.log(userInfo);
        const file = `${__dirname}/public/Cultx.apk`;
        res.download(file);
    }
    catch (err){
        res.json({
            code: 400,
            msg: "无效令牌"
        })
    }
})

app.listen(port, () => {
    console.log('sever run to http://127.0.0.1:' + port);
})

8M服务器, 远程下载23m的文件. 无中断