简体   繁体   中英

Access file on a Node.js server

If you are familiar with NowJS, one you start an instance of Now on the server, you can access a file located on the server by <script src="http://localhost:8080/nowjs/now.js"></script>

Any ideas on how to implement this?

Thanks, Mark

There are many static file handler modules already in node, take a look at: https://github.com/joyent/node/wiki/modules#wiki-web-frameworks-static

Most popular are: https://github.com/felixge/node-paperboy and https://github.com/cloudhead/node-static

Using node static is as simple as:

var static = require('node-static');
var file = new(static.Server)('./public');

    require('http').createServer(function (request, response) {
        request.addListener('end', function () {

            file.serve(request, response);
        });
    }).listen(8080);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM