Serve static image using netlify?

409 Views Asked by At

I am currently using express and I want to host my website with netlify but the problem is I don't know how to serve static images with netlify. While normally running the app not using netlify everything works. Is there a way to serve static images while hosting nodejs app on netlify ?

const express = require("express");
const serverless = require("serverless-http");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();

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

  next();
});

app.set("view engine", "ejs");

app.use(express.static(path.join(__dirname, "public")));
app.use("/images", express.static(path.join(__dirname, "images")));

const rootDir = require("./util/path");

const errorController = require("./controllers/error");
const projectRoutes = require("./routes/project");
const { json } = require("body-parser");

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

app.use("/.netlify/functions/app", projectRoutes);

module.exports.handler = serverless(app);

enter image description here

Folder Structure

0

There are 0 best solutions below