Question

Node API served with NGINX cannot serve images

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?


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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