Search code examples
node.jsweb-applicationscode-structureapplication-structure

the application structure in node.js?


I am familiar with Java web containers, where web applications are deployed as war files.

I am confused how to deploy CSS, JS, HTML, images (and so on) in Node.js. How does one do this?

I have very limited knowledge of Node.js. Thanks in advance!


Solution

  • http://expressjs.com/

    http://expressjs.com/guide.html#configuration

    app.js

    app.configure(function(){
      var oneYear = 31557600000;
      app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
      app.use(express.errorHandler());
    });
    
    app.listen(8888);
    

    index.html

    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="css/index.css">
        </head>
        <body>
            <span class='hello'>Hello world!</span>
        </body>
    </html>
    

    index.css

    .hello { color: green; }
    

    Directory structure:

    project/
        app.js
        public/
            index.html
            css/
                index.css
    

    Run your app: node app.js

    Go visit your website: http://localhost:8888

    The directory and file names are arbitrary. Everything is configurable, nothing is complicated. Nobody's trying to keep you tied to a specific directory structure or naming scheme in node, generally speaking.

    Go get em, tiger.