Node.js 无法在heroku中插入或显示express.js api中的图像

Node.js 无法在heroku中插入或显示express.js api中的图像,node.js,mongodb,express,heroku,Node.js,Mongodb,Express,Heroku,我有一个使用express.js创建的api,它包含上传和显示图像,它在本地主机和heroku本地web中都是完美的word,但在heroku cloud的同一个项目中,除了未显示的图像外,其他一切都正常,我不知道图像是否插入文件夹,因为我看不到heroku中的文件或文件夹。。。。这是我在本地heroku上传图像的代码: const filter = (req, file, cb)=>{ if(file.mimetype == "image/jpg" || fi

我有一个使用express.js创建的api,它包含上传和显示图像,它在本地主机和heroku本地web中都是完美的word,但在heroku cloud的同一个项目中,除了未显示的图像外,其他一切都正常,我不知道图像是否插入文件夹,因为我看不到heroku中的文件或文件夹。。。。这是我在本地heroku上传图像的代码:

const filter = (req, file, cb)=>{
    if(file.mimetype == "image/jpg" || file.mimetype == "image/jpeg" || file.mimetype == "image/png"){
        cb(null, true)
    }else{
        cb(null, false);
    }
}

const upload = multer({
    filter: filter
});
app.post('/service' ,upload.single('file'), checkAdminAuth, (req, res, next)=>{
    try {
        req.file != null ?
        sharp(req.file.buffer).resize(200, 200).toFile('upload/' 
        +req.file.originalname,
         (err, resizeImage) => {
            if (err) {
                console.log(err);
            } else {
             //   fs.unlinkSync(req.file.path)
                console.log(resizeImage);
            }
            
        }) : ""
       // fs.unlinkSync(req.file.path)
       next()
        return res.status(201).json({
            message: 'success'
        });
    } catch (error) {
        console.error(error);
    }
});

app.use('/upload', express.static('upload'));
/////////////// 这是来自heroku的日志:

2020-08-03T12:27:53.175084+00:00 heroku[web.1]: State changed from starting to up
2020-08-03T12:27:54.300490+00:00 app[web.1]: ::ffff:10.69.19.99 - - [03/Aug/2020:12:27:54 +0000] "GET /service/cat/5f27f0f6d3b2d900249dbcb7 HTTP/1.1" 200 329
2020-08-03T12:27:54.301859+00:00 heroku[router]: at=info method=GET path="/service/cat/5f27f0f6d3b2d900249dbcb7" host=infinite-meadow-47540.herokuapp.com request_id=3fd0deaa-3237-4150-8e42-08aa484baebe fwd="185.95.207.81" dyno=web.1 connect=0ms service=32ms status=200 bytes=686 protocol=https
2020-08-03T12:27:55.404054+00:00 app[web.1]: ::ffff:10.155.88.71 - - [03/Aug/2020:12:27:55 +0000] "GET /service/cat/5f27f0f6d3b2d900249dbcb7 HTTP/1.1" 200 329
2020-08-03T12:27:55.404547+00:00 heroku[router]: at=info method=GET path="/service/cat/5f27f0f6d3b2d900249dbcb7" host=infinite-meadow-47540.herokuapp.com request_id=b4c72174-4e32-4013-be61-f39cd3311b53 fwd="185.95.207.81" dyno=web.1 connect=0ms service=6ms status=200 bytes=686 protocol=https
2020-08-03T12:27:56.443405+00:00 app[web.1]: ::ffff:10.45.252.73 - - [03/Aug/2020:12:27:56 +0000] "GET /service/cat/5f27f0f6d3b2d900249dbcb7 HTTP/1.1" 200 329
2020-08-03T12:27:56.446998+00:00 heroku[router]: at=info method=GET path="/service/cat/5f27f0f6d3b2d900249dbcb7" host=infinite-meadow-47540.herokuapp.com request_id=2c77f41c-4bc4-4f52-b8ed-885d0c7e9544 fwd="185.95.207.81" dyno=web.1 connect=0ms service=5ms status=200 bytes=686 protocol=https
2020-08-03T12:27:56.819064+00:00 heroku[router]: at=info method=GET path="/upload/image_picker8301926133547768645.jpg" host=infinite-meadow-47540.herokuapp.com request_id=7d95912d-be4a-42a7-9b65-da69cd26a4b1 fwd="185.95.207.81" dyno=web.1 connect=0ms service=5ms status=404 bytes=573 protocol=https
2020-08-03T12:27:56.822720+00:00 app[web.1]: ::ffff:10.30.127.176 - - [03/Aug/2020:12:27:56 +0000] "GET /upload/image_picker8301926133547768645.jpg HTTP/1.1" 404 181
2020-08-03T12:28:00.626092+00:00 heroku[router]: at=info method=GET path="/cat" host=infinite-meadow-47540.herokuapp.com request_id=e836406a-c878-4cde-a649-05757d0bdc5b fwd="185.95.207.81" dyno=web.1 connect=5ms service=6ms status=200 bytes=848 protocol=https
2020-08-03T12:28:00.627757+00:00 app[web.1]: ::ffff:10.79.206.66 - - [03/Aug/2020:12:28:00 +0000] "GET /cat HTTP/1.1" 200 491
2020-08-03T12:28:00.642170+00:00 app[web.1]: ::ffff:10.9.247.118 - - [03/Aug/2020:12:28:00 +0000] "GET /cat HTTP/1.1" 200 491
2020-08-03T12:28:00.644944+00:00 heroku[router]: at=info method=GET path="/cat" host=infinite-meadow-47540.herokuapp.com request_id=560a35d0-df32-463b-b8c1-eb4823afa3f8 fwd="185.95.207.81" dyno=web.1 connect=0ms service=7ms status=200 bytes=848 protocol=https
2020-08-03T12:28:01.583557+00:00 heroku[router]: at=info method=GET path="/cupload/139585975-e-learning-concept-with-blurred-city-abstract-lights-background.jpg" host=infinite-meadow-47540.herokuapp.com request_id=46562fa8-ac78-4f59-897e-0e44a2b6901e fwd="185.95.207.81" dyno=web.1 connect=0ms service=14ms status=404 bytes=616 protocol=https
2020-08-03T12:28:01.585907+00:00 app[web.1]: ::ffff:10.63.145.147 - - [03/Aug/2020:12:28:01 +0000] "GET /cupload/139585975-e-learning-concept-with-blurred-city-abstract-lights-background.jpg HTTP/1.1" 404 224
2020-08-03T12:28:06.990644+00:00 app[web.1]: ::ffff:10.45.155.176 - - [03/Aug/2020:12:28:06 +0000] "GET /cupload/139585975-e-learning-concept-with-blurred-city-abstract-lights-background.jpg HTTP/1.1" 404 224
2020-08-03T12:28:06.998251+00:00 heroku[router]: at=info method=GET path="/cupload/139585975-e-learning-concept-with-blurred-city-abstract-lights-background.jpg" host=infinite-meadow-47540.herokuapp.com request_id=b9e68097-d78c-4d5e-b9da-08a5a6779bdd fwd="185.95.207.81" dyno=web.1 connect=0ms service=8ms status=404 bytes=616 protocol=https
2020-08-03T12:28:30.181544+00:00 app[web.1]: Error: ENOENT: no such file or directory, open 'upload/b858c17a46a29f9d7ddff5779df988dd'
2020-08-03T12:28:30.186011+00:00 heroku[router]: at=info method=POST path="/service" host=infinite-meadow-47540.herokuapp.com request_id=66638e1f-8082-4624-ad3b-becae5428427 fwd="185.95.207.81" dyno=web.1 connect=1ms service=10241ms status=500 bytes=404 protocol=https
2020-08-03T12:29:21.876414+00:00 app[web.1]: Error: ENOENT: no such file or directory, open 'upload/8e8c7129133e0d2819bd1299d922767b'
2020-08-03T12:29:21.878333+00:00 heroku[router]: at=info method=POST path="/service" host=infinite-meadow-47540.herokuapp.com request_id=2b6a2985-ceb8-4774-8000-4b320ef162f4 fwd="185.95.207.81" dyno=web.1 connect=0ms service=11393ms status=500 bytes=404 protocol=https
2020-08-03T13:01:53.096720+00:00 heroku[web.1]: Idling
2020-08-03T13:01:53.098427+00:00 heroku[web.1]: State changed from up to down
2020-08-03T13:01:54.003136+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-08-03T13:01:54.083729+00:00 heroku[web.1]: Process exited with status 143
2020-08-03T13:43:12.000000+00:00 app[api]: Build started by user osama.cs@cihanuniversity.edu.iq
2020-08-03T13:43:38.449759+00:00 app[api]: Deploy 40ec5723 by user osama.cs@cihanuniversity.edu.iq
2020-08-03T13:43:38.449759+00:00 app[api]: Release v9 created by user osama.cs@cihanuniversity.edu.iq
2020-08-03T13:43:38.613016+00:00 heroku[web.1]: State changed from down to starting
2020-08-03T13:43:41.000000+00:00 app[api]: Build succeeded

Heroku文件系统是短暂的——这意味着在dyno运行时对文件系统的任何更改只会持续到dyno关闭或重新启动。每个dyno都会使用最近部署的文件系统的干净副本启动。这类似于许多基于容器的系统(如Docker)的操作

此外,在正常运行情况下,dynos每天都会在一个称为“循环”的过程中重新启动

这两个事实意味着Heroku上的文件系统不适合持久存储数据。在需要存储数据的情况下,我们建议使用数据库插件,如Postgres(用于数据)或专用文件存储服务,如AWS S3(用于静态文件)。如果您不想在AWS上建立一个帐户来创建S3 bucket,我们这里也有处理静态资产存储和处理的插件


阅读一下

那么,我什么时候可以免费测试我的应用程序的全部功能呢?我想你可以使用Cloudinary。它的入门计划(10GB存储)是免费的。