Home > Software engineering >  Can't get Serving Static Files using node js (express)
Can't get Serving Static Files using node js (express)

Time:04-05

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 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

CodePudding user response:

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'));

CodePudding user response:

Try to access it like this :

/Minions_characters.png

  • Related