Basic static file server in NodeJS

后端 未结 8 767
盖世英雄少女心
盖世英雄少女心 2020-11-28 20:06

I\'m trying to create a static file server in nodejs more as an exercise to understand node than as a perfect server. I\'m well aware of projects like Connect and node-stati

8条回答
  •  北海茫月
    2020-11-28 20:25

    @JasonSebring answer pointed me in the right direction, however his code is outdated. Here is how you do it with the newest connect version.

    var connect = require('connect'),
        serveStatic = require('serve-static'),
        serveIndex = require('serve-index');
    
    var app = connect()
        .use(serveStatic('public'))
        .use(serveIndex('public', {'icons': true, 'view': 'details'}))
        .listen(3000);
    

    In connect GitHub Repository there are other middlewares you can use.

提交回复
热议问题