Node API served with NGINX cannot serve images

Posted January 12, 2021 133 views

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?

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.

Submit an Answer
2 answers

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.

Show answer This answer has been marked as resolved by andrchas.