Node API served with NGINX cannot serve images

  • Posted January 12, 2021
  • Nginx

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');

app.use(bodyParser.urlencoded({ extended: false }));

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

... Some Routes

My default

server {
        listen 80;

  	access_log  /var/log/nginx/example.com_access.log;
  	error_log   /var/log/nginx/example.com_error.log;	
	location / {
                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?


Submit an 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.

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!

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