Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm not able to solve an error in nodejs using express framework

I'm a totally beginner with node.js and express framework. I'm following these tutorials.

Here's my code:

var fs = require("fs");
var config = JSON.parse(fs.readFileSync(__dirname + "\\config.json"));
var host = config.host;
var port = config.port;
var express = require("express");

var app = express.createServer();

app.get(__dirname + "\\",function(request, response){
    response.send("Hello!");
});

app.listen(port,host);

And I'm having this error and I'm not able to solve it:

C:\Users\Fabio\Documents\GitHub\NodeJSProjects\ExpressSimpleServer\Server.js:7
var 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> (C:\Users\Fabio\Documents\GitHub\NodeJSProjects\ExpressSimpleServer\Server.js:7: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'm using node.js 0.10.26, express 4.0.0 and windows 8

like image 922
Tinwor Avatar asked Jul 06 '26 00:07

Tinwor


2 Answers

See express project's reference. CreateServer is deprecated. Simply do this to create server and listen to a port Var app = require("express")(); app.listen(3000);

like image 105
Pradeep Sharma Avatar answered Jul 08 '26 14:07

Pradeep Sharma


SOLVED
I change the code according to expressjs guide

var fs = require("fs");
var config = JSON.parse(fs.readFileSync(__dirname + "\\config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.get('/hello.txt', function(req, res){
    res.send('Hello World');
});
var server = app.listen(port, function() {
    console.log('Listening on port %d', server.address().port);
});
like image 21
Tinwor Avatar answered Jul 08 '26 13:07

Tinwor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!