Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Connect error - has no method 'logger'

Tags:

node.js

Fairly new to nodejs here. I have a nodejs webserver running that serves up data via ajax. I now need to extend the server to respond to various post requests. All I am trying to get running is the bodyParser middleware but on a different development pc.

os: debian nodejs: v0.10.29

have run "npm install connect" as normal user and root

code

var http = require('http');
var connect = require('connect');

var app = connect()
  .use(connect.logger('dev'))
  .use(connect.static('public'))
  .use(connect.bodyParser())
  .use(function(req, res){
    res.end(JSON.stringify(req.body));
  })

http.createServer(app).listen(2081);

getting this output carl@crunchbang:~/nodedev$ node ./test.js

/home/carl/nodedev/test.js:5
  .use(connect.logger('dev'))
               ^
TypeError: Object function createServer() {
  function app(req, res, next){ app.handle(req, res, next); }
  merge(app, proto);
  merge(app, EventEmitter.prototype);
  app.route = '/';
  app.stack = [];
  return app;
} has no method 'logger'
    at Object.<anonymous> (/home/carl/nodedev/test.js:5:16)
    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:906:3

I know im missing something really simple any help appreciated.

like image 733
Carlhako Avatar asked Jul 11 '14 07:07

Carlhako


2 Answers

Connect no longer comes with bundled middleware. Your code would look something like this with the middleware included.

var http = require('http');
var connect = require('connect');
var bodyParser = require('body-parser');
var logger = require('morgan');

var app = connect()
  .use(logger())
  .use(connect.static('public'))
  .use(bodyParser.urlencoded({
      extended: true
  }))
  .use(bodyParser.json())
  .use(function(req, res){
    res.end(JSON.stringify(req.body));
  })

http.createServer(app).listen(2081);
like image 196
Ben Fortune Avatar answered Oct 12 '22 08:10

Ben Fortune


As you can see in the documentation : https://www.npmjs.org/package/connect

morgan - previously logger

Now you need to use morgan.

like image 27
Yanozu Avatar answered Oct 12 '22 07:10

Yanozu