Displaying an image with EJS in node.js/express

前端 未结 3 1010
深忆病人
深忆病人 2021-02-07 05:16

I\'m just trying to get setup with node.js/express/ejs. I know ejs isn\'t actual HTML and so I\'m having a hard time just displaying a simple image. Can someone point me in the

相关标签:
3条回答
  • 2021-02-07 05:25

    My js file was here c:/blog/index.js and image file c:/blog/views/image.js. And entered js file this code

    app.use( express.static( "views" ) );

    then in CSS added property

    body {
    background-image: url("./image.jpg"); }

    0 讨论(0)
  • 2021-02-07 05:41

    Static files in Express must go inside the directory specified in your static middleware. This is commonly ./public/.

    For example, in your server.js you may have something like this:

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

    Each file inside this folder will be accessible from the root URL, so this will work:

    <img src="logo.jpg" />
    
    0 讨论(0)
  • 2021-02-07 05:45

    You have to assign app.use( express.static( "public" ) ); on app.js then don't forget the / as root:

    <img src="/images/logo.jpg" />
    

    the images folder should be in public folder:

    - public/
      - images/
        - logo.png
    - app.js
    
    0 讨论(0)
提交回复
热议问题