0

What is the correct code format to establish the static server from nodejs

This is my current code to declare the static server and won't work

app.use(express.static( './src/uploads'));

My URL that used to be fetch by Vuejs from nodejs folder

http://localhost:5000/src/uploads/100-cities-with-the-most-beautiful-women-in-the-world.jpg

As you can see on the image this is the folder structure

-ecommerce-backend

 -src

  -uploads

As you can

4
  • 1
    Did you tried this url http://localhost:5000/100-cities-with-the-most-beautiful-women-in-the-world.jpg Commented Aug 5, 2019 at 5:17
  • yes and it says Cannot GET Commented Aug 5, 2019 at 5:18
  • where is your main file app.js located inside src folder or outside? If it's inside path must be wrong. Commented Aug 5, 2019 at 5:35
  • it is located outside the src. Commented Aug 5, 2019 at 5:44

1 Answer 1

1

A working code used in one of my projects is

var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));

You can replace public with your dir name. The directory structure for the project is

-routes
-models
-public
app.js

Nodejs version is

node -v
v10.16.0
Sign up to request clarification or add additional context in comments.

3 Comments

Like this? localhost:5000/public/100-cities-with-the-most-beautiful-women-in-the-world.jpg
No need to include the public in the path. localhost:5000/myfile.jpg
Sir, salute to you, been trying to solve this problem since yesterday. thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.