GithubHelp home page GithubHelp logo

atsuya / static-resource Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 181 KB

Static resource handling for node.js

Home Page: http://github.com/atsuya/static-resource

License: MIT License

JavaScript 100.00%

static-resource's Introduction

Static Resource: node.js module for static resource handling

The Static Resource is a module for node.js to handle static resources such as html, image, css files.

How to use

var http = require('http');
var fs = require('fs');
var url = require('url');
var staticResource = require('../');

// passing where is going to be the document root of resources.
var handler = staticResource.createHandler(fs.realpathSync('./static'));

var server = http.createServer(function(request, response) {
    var path = url.parse(request.url).pathname;
    // handle method returns true if a resource specified with the path
    // has been handled by handler and returns false otherwise.
    if(!handler.handle(path, request, response)) {
        response.writeHead(404);
        response.write('404');
        response.end();
    }
});
server.listen(8080);

Static Resource is designed to handle resources you choose to let Static Resource handle and don't do anything else. This leaves you with a choice of handling certain resources by yourself yet letting other resources handled by Static Resource.

Adding/removing content types is easy.

var handler = staticResource.createHandler(fs.realpathSync('./static'));
handler.addContentType('.zip', 'application/zip');
handler.removeContentType('.html');

For adding a new content type, just pass file extension with '.' and content type along with it. For removing existing content type, you only need to pass the file extension.

The content types that I think very common are added by default in its constructor, so add/remove by yourself. When it doesn't know which content type to use, it defaults to application/octet-stream.

Notes

Static Resource is meant to be experimental! It might be useful for testing but is not probably a good idea to use it in production.

Demo

To run a demo, go to example directory and run the following command:

$ node server.js

Then open up web browser and go to http://localhost:8080/index.html. That index.html is handled by Static Resource.

Credits

Socket.IO Node.JS Server - I followed his model for the layout of this module, and this README.

License

Static Resource is licensed under the MIT license.

The MIT License

Copyright (c) 2010 Atsuya Takagi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

static-resource's People

Contributors

atsuya avatar

Stargazers

Jared Wright avatar Stan avatar Lenny Nilsson avatar Michael F avatar  avatar Arif Amirani avatar KOBA789 avatar Silas Ribas Martins avatar  avatar Régis Gaidot avatar Chris Sternal-Johnson avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

kontinuity

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.