当服务器在我的笔记本电脑上运行时,我可以在本地获取图像。但我无法从部署的服务器获取图像。 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}...`));
我也尝试过在循环上部署。但一切都是徒劳
嘿伙计,你明白了吗?
我无法发送至