Home > database >  Getting "Cannot GET /uploads/" for static file in Node js
Getting "Cannot GET /uploads/" for static file in Node js

Time:08-25

import express from "express";
import path from "path";
import cors from "cors";
import bodyParser from "body-parser";
import {fileURLToPath} from 'url';
import fileRoutes from "./routes/fileuploadRoutes.js"
const app = express();
app.use(cors());
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// console.log((path.join(__dirname, 'uploads')))
app.use(bodyParser.json());
app.use('/uploads', express.static(path.join(__dirname, '/uploads')))

I Used the above code and not able to get the static uploads folder at "http://localhost:3000/uploads/" where 3000 is the port defined. enter image description here

CodePudding user response:

can you please try once with the following code?

import express from "express";
import path from "path";
import cors from "cors";
import bodyParser from "body-parser";
import {fileURLToPath} from 'url';
import fileRoutes from "./routes/fileuploadRoutes.js"
const app = express();
app.use(cors());
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// console.log((path.join(__dirname, 'uploads')))
app.use(bodyParser.json());
app.get('/uploads', express.static(path.join(__dirname, '/uploads')))

CodePudding user response:

Got the answer, finally tried with axios api call

  • Related