Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't get Serving Static Files using node js (express)

I have learning Node JS and try to open the Serving Static Files using the below function on node js express. But I got the Cannot GET error check the screenshot

I have a proper folder structure.

I refer to the below article
https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

app.use(express.static('public'));

Here is the full code

var express = require('express');
var app = express();

app.use(express.static('public'));

app.get('/', function (req, res) {
   res.send('Hello World');
})

var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port

   console.log(host, port)
})

Folder structure

node_modules
server.js
public/
public/images
public/images/Minions_characters.png

>Solution :

app.use(express.static('public'));

You didn’t specify a path as the first argument to use.

You’ve mounted the contents of the public directory at /

Therefore the correct URL does not start with /public


Either:

  • remove /public from the URL
  • add /public to the mount point: app.use('/public/, express.static('public'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading