I have the node js code like this:
var express = require('express')
, app = express.createServer();
app.use(express.bodyParser());
app.post('/', function(request, response){
console.log(request.body); // your JSON
response.send(request.body); // echo the result back
});
app.listen(3000);
when I run I get the error like this:
, app = express.createServer();
^
TypeError: Object function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
} has no method 'createServer'
at Object.<anonymous> (/Users/anto_belgin/programs/node/appleconnect.js:2:19)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
I have installed express using:
npm install express
Is there anything I need to do get it working?
In my express 4 app, I'm doing this:
var express = require('express');
var app = express();
var server = app.listen(8081, function() {});
Since express changed a bit from v3 to v4, make sure you're looking at instructions for the version you are running.
I think createServer() is something you do on the http module, whereas express runs at a higher level on top of that so if you're starting a server using the express module, you do it differently.
From the Express documentation, app.listen() is a convenience method that replaces this:
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};