如何在node.js服务器中运行静态html文件

时间:2012-07-25 16:37:07

标签: node.js static

我是node.js的新手,我已经学习了node.js并表达了框架

我知道如何基本运行服务器,设置路由器和渲染html模板,但是所有教程告诉我通过res.send()render渲染html如何查看静态文件?如静态sample.html :

就像http://127.0.0.1:8000/static.html

一样

我的文件树如:

root
|
|---server.js
|---static
    |---static.html

如何通过设置路由器来查看静态文件?或者使用一些中间件(更好的使用快速框架)?

更新 我试过这个:

    var express = require('express');
    var app = express.createServer();
    app.get('/', function(req, res){
        res.send('Hello World');
    });
    app.configure(function () {
        app.use(express.static(__dirname + '/static'));
    })

    app.listen(8000);

但它仍然无效:告诉我:Cannot GET /static/client.html

2 个答案:

答案 0 :(得分:3)

您可以使用static middleware

执行此操作
app.use(express.static(__dirname + '/static'));

答案 1 :(得分:1)

您的文件是否在更新中指定为client.html,还是在文件树中显示为static.html

所以,试试:

http://localhost:8000/client.html

...或

http://localhost:8000/static.html

注意缺少“/ static”目录。

另外,将app.configure行放在app.get之前。