user3511563
user3511563

Reputation: 397

Object has no method use

I'm trying to follow this tutorial, on hosting Cocos2d in NodeJS, but I'm getting this error message:

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

in this line:

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

This is my code:

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

Upvotes: 0

Views: 144

Answers (1)

shawnzhu
shawnzhu

Reputation: 7585

The error message tells you what happened: the variable server refers to a web server object of Node, not express application. so your code should be corrected to:

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

Upvotes: 2

Related Questions