Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象没有方法用途_Javascript_Node.js - Fatal编程技术网

Javascript 对象没有方法用途

Javascript 对象没有方法用途,javascript,node.js,Javascript,Node.js,我正试图遵循教程,在NodeJS中托管Cocos2d,但我收到了以下错误消息: Object #<Server> has no method 'use' at Object.<anonymous> 这是我的代码: var express = require('express'), http = require('http'); var app = express(); app.use(express.bodyParser()); var server

我正试图遵循教程,在NodeJS中托管Cocos2d,但我收到了以下错误消息:

Object #<Server> has no method 'use'
    at Object.<anonymous>
这是我的代码:

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);

错误消息告诉您发生了什么:变量
server
指的是节点的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);

看起来您并没有非常仔细地学习本教程。您正在使用
var server=http.createServer(app),但本教程使用
server=express.createServer()。但是,本教程已有两(!)年历史,因此您最好直接阅读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);