The original API for obtaining pictures failed to load after using connect-history-api-fallback middleware in the node express project.

as the title

< H2 > the interface code is as follows < / H2 >
/ get /avatar/file_id
router.get("/avatar", async (req, res, next) => {
    const id = req.query.file_id
    const url = await FileModel.getFilePath(id)
    res.set("content-type", "image/jpg")
    let stream = fs.createReadStream(url)
    let responseData = []; // 
    if (stream) { // 
        stream.on("data", chunk => {
            responseData.push(chunk)
        })
        stream.on("end", () => {
            let finalData = Buffer.concat(responseData)
            res.write(finalData)
            res.end();
        });
    }
})

the FileModel.getFilePath code is as follows

// 
handleFile.getFilePath = async function  (id) {
    let fileObject = await handleFile.getFileById(id)
    return path.join(__dirname, `../uploads/${fileObject.filename}`)
}

index.js introduces the history code as follows

const express = require("express")
const session = require("express-session")
const bodyParser = require("body-parser")
const routes = require("./routes")
const history = require("connect-history-api-fallback")
const path = require("path")
const favicon = require("serve-favicon")

const app = new express()

app.use(history())
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static(path.join(__dirname, "../dist")))
app.use(favicon(path.join(__dirname, "./favicon.ico")))



const sessionStore = new session.MemoryStore({ reapInterval: 3600 * 1000 })
app.use(session({
    secret: "Stefanie Sun",
    store: sessionStore,
    resave: true, //  session
    saveUninitialized: true,  // 
    cookie: { maxAge: 3600 * 1000 }, // 
    rolling: true
}))


routes(app)

but using the API to get pictures in the interface took 304 and could not get the file

clipboard.png

everything will be fine after I delete history
how to solve this

Mar.11,2021

solved, in fact, all get requests will become index, after using connect-history-api-fallback middleware, so get requests are not executed at all. Just set rewrites

app.use(history({
    rewrites: [
      {
        from: /^\/api\/.*$/,
        to: function(context) {
            return context.parsedUrl.path
        }
      }
    ]
  }))

when rewrites is set, it means that all get with api are proxied to parsedUrl.path, which is actually the original path

.

what does this package do? can it be used with stream streams

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36597-4ec6f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36597-4ec6f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?