簡體   English   中英

對象沒有方法使用

[英]Object has no method use

我正在嘗試在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);

錯誤消息告訴您發生了什么:變量server引用Node的Web服務器對象,而不是快速應用程序。 因此您的代碼應更正為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM