简体   繁体   中英

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!

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.

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