为什么在 Vercel 上部署后无法从服务器获取图像?

问题描述 投票:0回答:1

当服务器在我的笔记本电脑上运行时,我可以在本地获取图像。但我无法从部署的服务器获取图像。 index.js的代码:

require("dotenv").config();
const upload = require("./routes/upload");
const Grid = require("gridfs-stream");
const mongoose = require("mongoose");
const connection = require("./db");
const express = require("express");
const app = express();

let gfs;
connection();

const conn = mongoose.connection;
conn.once("open", function () {
    gfs = Grid(conn.db, mongoose.mongo);
    gfs.collection("photos");
});

app.use("/file", upload);

// media routes
app.get("/file/:filename", async (req, res) => {
    try {
        const file = await gfs.files.findOne({ filename: req.params.filename });
        const readStream = gfs.createReadStream(file.filename);
        readStream.pipe(res);
    } catch (error) {
        res.send("not found");
    }
});

app.delete("/file/:filename", async (req, res) => {
    try {
        await gfs.files.deleteOne({ filename: req.params.filename });
        res.send("success");
    } catch (error) {
        console.log(error);
        res.send("An error occured.");
    }
});

const port = process.env.PORT || 8080;
app.listen(port, console.log(`Listening on port ${port}...`));

我也尝试过在循环上部署。但一切都是徒劳

node.js mongodb express deployment image-upload
1个回答
0
投票

嘿伙计,你明白了吗?

我无法发送至

© www.soinside.com 2019 - 2024. All rights reserved.