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?

The developer cloud

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

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.