Report this

What is the reason for this report?

Node API served with NGINX cannot serve images

Posted on January 12, 2021

I have built an Ubuntu server and I have installed Node, Mongo, and NGINX.

I have a Node API server that interacts with MongoDB. I also have an Agular app that interacts with the API and successfully stores data in MongoDB.

But when I try to get the images that I stored through the API I get a 404 not found.

My Node server

const express       = require("express");
const app           = express();
const morgan        = require("morgan");
const bodyParser    = require("body-parser");
const mongoose      = require('mongoose')
const path          = require('path');


const port = 9000

... Mongoose connection


  app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
});

app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use('/images', express.static(path.join('uploads')))

... Some Routes

My default

server {
        listen 80;
        server_name domain.tk;

  	access_log  /var/log/nginx/example.com_access.log;
  	error_log   /var/log/nginx/example.com_error.log;	
        
	location / {
                proxy_pass http://127.0.0.1:9000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

... Some other server that I serve Angular app

And the folder structure is

- app.js
- api
- uploads
  -- some dynamic folders that the images stored inside

What am I doing wrong?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

What I could suggest is checking your browser web console to see the exact error on why the images are not loaded and also see the URL that the images are being pulled from.

Feel free to share the URL here. Regards, Bobby

Ok i actually found it. I learned the hard way that the ulr is case sensitive. But while i was running the app in localhost it was working that why i got confused!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.