对象没有使用方法

时间:2014-04-12 01:10:05

标签: javascript node.js

我正在尝试关注this教程,在NodeJS中托管Cocos2d,但我收到此错误消息:

Object #<Server> has no method 'use'
    at Object.<anonymous>

在这一行:

server.use('/Art', express.static(__dirname + '/Art') );

这是我的代码:

var express = require('express'),
    http = require('http');

var app = express();
app.use(express.bodyParser());
var server = http.createServer(app);

server.use('/Art', express.static(__dirname + '/Art') );
server.use('/Platform', express.static(__dirname + '/Platform') );
server.use('/Sounds', express.static(__dirname + '/Sounds') );
server.use('/Src', express.static(__dirname + '/Src') );

server.get('/', function(req,res){
    res.sendfile('index.html');
    console.log('Sent index.html');
});

server.get('/api/hello', function(req,res){
   res.send('Hello Cruel World');
});
server.listen(process.env.PORT || 3000);

1 个答案:

答案 0 :(得分:2)

错误消息告诉您发生了什么:变量server是指Node的Web服务器对象,而不是express应用程序。所以你的代码应该更正为:

app.use('/Art', express.static(__dirname + '/Art') );
app.use('/Platform', express.static(__dirname + '/Platform') );
app.use('/Sounds', express.static(__dirname + '/Sounds') );
app.use('/Src', express.static(__dirname + '/Src') );

app.get('/', function(req,res){
    res.sendfile('index.html');
    console.log('Sent index.html');
});

app.get('/api/hello', function(req,res){
    res.send('Hello Cruel World');
});

http.createServer(app).listen(process.env.PORT || 3000);