简体   繁体   English

无法读取express.js中上传的图像

[英]Can not read uploaded images in express.js

In my REST Api,I am uploading some images. 在我的REST Api中,我正在上传一些图像。 This is my project stucture. 这是我的项目结构。 第一结构图

Now inside the public folder there is another folder called images. 现在,公用文件夹中还有另一个名为images的文件夹。 This is where I upload the images. 这是我上传图像的地方。 第二结构图

However for some reason when I typing the locahost url to a specific image ie http://127.0.0.1:61592/larissaApp/public/images/myImage-1497186011532.jpg I get this message in the browser. 但是由于某种原因,当我将locahost网址键入特定图像时,即http://127.0.0.1:61592/larissaApp/public/images/myImage-1497186011532.jpg,我在浏览器中收到了此消息。

Cannot GET /larissaApp/public/images/myImage-1497186011532.jpg

This is my app.js file. 这是我的app.js文件。

            var express = require('express');
            var path = require('path');
            var favicon = require('serve-favicon');
            var logger = require('morgan');
            var cookieParser = require('cookie-parser');
            var bodyParser = require('body-parser');
            var mongoose = require('mongoose');
            var url = 'mongodb://localhost:27017/larissaApp';

            mongoose.connect(url);
            var db = mongoose.connection;
            db.on('error',console.error.bind(console,'connection error:'));
            db.once('open',function(){
               console.log("Connected correctly to server"); 
            });

            var routes = require('./routes/index');
            var users = require('./routes/users');
            var newsRouter = require('./routes/newsRouter');
            var cityRouter = require('./routes/cityRouter');
            var imageRouter = require('./routes/imageRouter');

            var app = express();
            // view engine setup
            app.set('views', path.join(__dirname, 'views'));
            app.set('view engine', 'jade');
            // uncomment after placing your favicon in /public
            //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
            app.use(logger('dev'));

            app.use(bodyParser.urlencoded({ extended: false }));
            app.use(cookieParser());
            app.use(express.static(path.join(__dirname, 'public')));
            app.use(express.static(path.join(__dirname, 'public/images')));
            app.use('/', routes);
            app.use('/users', users);
            app.use('/news',newsRouter);
            app.use('/news',newsRouter);
            app.use('/city',cityRouter);
            app.use('/image',imageRouter);

            // catch 404 and forward to error handler
            app.use(function(req, res, next) {
              var err = new Error('Not Found');
              err.status = 404;
              next(err);
            });
            // error handlers
            // development error handler
            // will print stacktrace
            if (app.get('env') === 'development') {
              app.use(function(err, req, res, next) {
                res.status(err.status || 500);
                res.render('error', {
                  message: err.message,
                  error: err
                });
              });
            }
            // production error handler
            // no stacktraces leaked to user
            app.use(function(err, req, res, next) {
              res.status(err.status || 500);
              res.render('error', {
                message: err.message,
                error: {}
              });
            });
            module.exports = app;

I was hoping that this line of code would fix that error. 我希望这行代码可以解决该错误。

app.use(express.static(path.join(__dirname, 'public/images')));

but it doesn't. 但事实并非如此。 Any ideas? 有任何想法吗?

Thanks, 谢谢,

Theo. 西奥

This is due to the nature of how express.static() works. 这是由于express.static()工作原理所致。 If you take this bare example 如果您举这个简单的例子

const express = require('express');
const path = require('path');

const app = express();

app.use(express.static(path.join(__dirname, 'public')));

app.listen(8080);

Going to localhost:8080/public/images/myImage-1497186011532.jpg will produce the same error, however localhost:8080/images/myImage-1497186011532.jpg will work. 转到localhost:8080/public/images/myImage-1497186011532.jpg将产生相同的错误,但是localhost:8080/images/myImage-1497186011532.jpg将起作用。

If you want all the static files to be served on a certain route, like in your exmaple, you'll want to replace the above line with app.use('/public', express.static(path.join(__dirname, 'public'))); 如果您希望所有静态文件都按特定路径提供,例如在示例中,则需要用app.use('/public', express.static(path.join(__dirname, 'public')));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM