node.js中的应用程序结构?

Pre*_*thi 0 web-applications code-structure node.js application-structure

我熟悉Java Web容器,其中Web应用程序作为war文件部署.

我很困惑如何在Node.js中部署CSS,JS,HTML,图像(等等).怎么做到这一点?

我对Node.js的了解非常有限.提前致谢!

Jos*_*osh 5

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);
Run Code Online (Sandbox Code Playgroud)

的index.html

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>
    <body>
        <span class='hello'>Hello world!</span>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

index.css

.hello { color: green; }
Run Code Online (Sandbox Code Playgroud)

目录结构:

project/
    app.js
    public/
        index.html
        css/
            index.css
Run Code Online (Sandbox Code Playgroud)

运行你的应用: node app.js

去访问您的网站: http://localhost:8888

目录和文件名是任意的.一切都是可配置的,没有什么是复杂的.一般来说,没有人试图让你与节点中的特定目录结构或命名方案联系在一起.

去吧,老虎.