【发布时间】:2021-11-02 09:17:22
【问题描述】:
当我们导航到
时,使文件/图像可供访问localhost:8000/properties/files/[image name]
localhost:8000/properties/files/1635843023660-profile.jpg
文件结构
node_modules
src/
uploads/
1635843023660-profile.jpg
1635843023668-home.jpg
...
.env
package.json
index.js
import Fastify from "fastify";
import FastifyStatic from "fastify-static";
import path from "path";
import { propertiesRoutes } from "./routes/properties.js";
...
export const fastify = await Fastify({ logger: process.env.LOGGER || true });
const __dirname = path.resolve(path.dirname(""));
fastify.register(FastifyStatic, {
root: path.join(__dirname, "uploads"),
});
fastify.register(propertiesRoutes, { prefix: "/properties" });
...
routes/properties.js
export const propertiesRoutes = function (fastify, opts, done) {
...
fastify.get("/images/:name", (req, res) => {
res.sendFile(process.cwd() + "/uploads/" + req.params.name);
});
done();
};
现在我得到了
{"message":"Route GET:/properties/images/1635843023660-profile.jpg 不是 找到","错误":"未找到","statusCode":404}
【问题讨论】:
标签: javascript node.js fastify