Express Static nodejs

后端 未结 3 1350
[愿得一人]
[愿得一人] 2020-12-07 03:53

There is a style.css in public, but I can\'t seem to make the express static option work. I deleted express and have done npm i

相关标签:
3条回答
  • 2020-12-07 04:24
    app.use(express.static('public'));
    

    http://expressjs.com/4x/api.html#express.static

    0 讨论(0)
  • 2020-12-07 04:29

    Works just fine for me.

    app.js

    var express = require('express')
      , app = express.createServer();
    
    app.use(express.static(__dirname+'/public'));
    
    app.listen(8080, "127.0.0.1");
    

    mkdir public
    cd public
    touch README
    

    README

    test
    

    $ curl http://localhost:8080/README
    test
    

    $ npm ls
    connect@1.6.1
    express@2.4.4
    
    0 讨论(0)
  • 2020-12-07 04:41

    An important detail that I have overlooked on multiple occasions now is that "public" is not in the URL of the static content you are delivering.

    Alfred example nails it but it is easy to miss. The location of the README file is not localhost:8080/public/README, but simply localhost:8080/README

    0 讨论(0)
提交回复
热议问题