1

我正在尝试在 express3 中路由文件,但我遇到了问题。
所以这是路由文件的代码 -

var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

server.listen(8080);

// routing
app.get('/', function (req, res) {
    res.sendfile("index/index.html");
    app.use(app.static(__dirname + 'index'));
});

当我localhost:8080在 Chrome 中打开时,它给了我一个错误:

TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'static'

我做错了什么?

我所有的 HTML/CSS/JS 文件都在 index 目录中。

4

1 回答 1

1

static是 express 的静态函数,您不能从 express 创建的实例对象访问它。您需要将所需的表达分配给不同的变量。

var express = require('express'),
    app = = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(8080);

// routing
app.get('/', function (req, res) {
    res.sendfile("index/index.html");
    app.use(express.static(__dirname + 'index'));
});
于 2013-02-20T09:07:41.273 回答