使用 multer 在 nodejs 中上传图像后 json 文件中的图像 url 出现问题
problem with the image url in json file after using multer to upload image in nodejs
在使用 postman it returns 将图像的文件名上传到 data.json 带有“uploads\”的文件后,我正在使用 multer 为我的博客网站上传图像。
如何在 data.json 文件中获取“上传/”而不是“上传\”?
data.json
{
"id": "1610726046543",
"title": "title",
"content": "content",
"post_image": "uploads\post-image-1610726046051.jpg",
"added_date": "1610726046543"
}
这是 app.js 文件,通过它我使用 Postman API 应用程序获取有关新 post 的信息,并将此新数据添加到 data.json 文件
app.js
const express = require("express");
const app = express();
const Post = require("./api/models/posts")
const postsData = new Post();
const multer = require('multer')
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function(req, file, cb) {
cb(null, `${file.fieldname}-${Date.now()}.jpg`)
}
})
const getExt = (mimetype) => {
switch(mimetype){
case "image/png":
return '.png';
case "image/jpeg":
return '.jpg';
}
}
var upload = multer({ storage: storage })
app.use((req, res, next)=>{
res.setHeader("Access-Control-Allow-Origin","*")
next()
})
app.use(express.json())
app.use('/uploads', express.static('uploads'))
app.get("/api/posts", (req, res)=>{
res.status(200).send(postsData.get())
})
app.get("/api/posts/:post_id", (req, res)=>{
const postId = req.params.post_id
const foundPost = postsData.getIndividualBlog(postId)
if(foundPost) {
res.status(200).send(foundPost)
}else{
res.status(404).send("Not Found")
}
})
app.post("/api/posts", upload.single("post-image"), (req,res)=>{
const newPost = {
"id": `${Date.now()}`,
"title": req.body.title,
"content": req.body.content,
"post_image": req.file.path,
"added_date": `${Date.now()}`
}
postsData.add(newPost)
res.status(201).send(newPost)
})
app.listen(3000, ()=>console.log("Listening on http://localhost:3000"));
我知道的不多node.js。但是您可以创建一个函数,在 post 完成后将所有“\”替换为“//”。我相信您可以先阅读 json 进行编辑,然后使用以下方法进行编辑:
JSON.parse(json);
只是简单地替换它?
app.post("/api/posts", upload.single("post-image"), (req,res)=>{
const newPost = {
"id": `${Date.now()}`,
"title": req.body.title,
"content": req.body.content,
"post_image": req.file.path.replace("\", "/"),
"added_date": `${Date.now()}`
}
postsData.add(newPost)
res.status(201).send(newPost)
})
在使用 postman it returns 将图像的文件名上传到 data.json 带有“uploads\”的文件后,我正在使用 multer 为我的博客网站上传图像。 如何在 data.json 文件中获取“上传/”而不是“上传\”?
data.json
{
"id": "1610726046543",
"title": "title",
"content": "content",
"post_image": "uploads\post-image-1610726046051.jpg",
"added_date": "1610726046543"
}
这是 app.js 文件,通过它我使用 Postman API 应用程序获取有关新 post 的信息,并将此新数据添加到 data.json 文件
app.js
const express = require("express");
const app = express();
const Post = require("./api/models/posts")
const postsData = new Post();
const multer = require('multer')
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function(req, file, cb) {
cb(null, `${file.fieldname}-${Date.now()}.jpg`)
}
})
const getExt = (mimetype) => {
switch(mimetype){
case "image/png":
return '.png';
case "image/jpeg":
return '.jpg';
}
}
var upload = multer({ storage: storage })
app.use((req, res, next)=>{
res.setHeader("Access-Control-Allow-Origin","*")
next()
})
app.use(express.json())
app.use('/uploads', express.static('uploads'))
app.get("/api/posts", (req, res)=>{
res.status(200).send(postsData.get())
})
app.get("/api/posts/:post_id", (req, res)=>{
const postId = req.params.post_id
const foundPost = postsData.getIndividualBlog(postId)
if(foundPost) {
res.status(200).send(foundPost)
}else{
res.status(404).send("Not Found")
}
})
app.post("/api/posts", upload.single("post-image"), (req,res)=>{
const newPost = {
"id": `${Date.now()}`,
"title": req.body.title,
"content": req.body.content,
"post_image": req.file.path,
"added_date": `${Date.now()}`
}
postsData.add(newPost)
res.status(201).send(newPost)
})
app.listen(3000, ()=>console.log("Listening on http://localhost:3000"));
我知道的不多node.js。但是您可以创建一个函数,在 post 完成后将所有“\”替换为“//”。我相信您可以先阅读 json 进行编辑,然后使用以下方法进行编辑:
JSON.parse(json);
只是简单地替换它?
app.post("/api/posts", upload.single("post-image"), (req,res)=>{
const newPost = {
"id": `${Date.now()}`,
"title": req.body.title,
"content": req.body.content,
"post_image": req.file.path.replace("\", "/"),
"added_date": `${Date.now()}`
}
postsData.add(newPost)
res.status(201).send(newPost)
})