Pablo
Pablo

Reputation: 10602

Express has no method createServer()

I'm creating a basic app trying to learn Node.js.

I need to load a start page in html (with some styles and js scripts).

In the future I will need to make ajax calls from js to Node, and I found I should use express extension.

I also found I could use express as a web server, to serve the start page (in html), to the client, I'm using this code:

var express = require('express');

var app = express.createServer();

app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
    res.render('index.html');
});

app.listen(8080, '127.0.0.1')

But in the second line, I get this error:

var app = express.createServer();
                  ^
TypeError: Object function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };

  mixin(app, EventEmitter.prototype, false);
  mixin(app, proto, false);

  app.request = { __proto__: req, app: app };
  app.response = { __proto__: res, app: app };
  app.init();
  return app;
} has no method 'createServer'

How can I load the page?

Upvotes: 1

Views: 1226

Answers (2)

Jorge Rivera
Jorge Rivera

Reputation: 775

express.createServer() has been deprecated. To create a server you need to do this:

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

var server = http.createServer(app);

Upvotes: 2

Nick Zuber
Nick Zuber

Reputation: 5637

I don't think express has an explicit function for creating a server; the way you would do this is simply define app as an express object, so just change this:

var app = express.createServer();

to this:

var app = express();

Upvotes: 1

Related Questions