Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

morgan deprecated expressjs

I'm learning nodejs an going through tutorial. I faced a problem that tutorial is for older version.

I have that code:

var express = require('express'),
    stylus = require('stylus'),
    logger = require('morgan'),
    bodyParser = require('body-parser');

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

function compile(str, path){
    return stylus(str).set('filename', path);
}

app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(logger);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(stylus.middleware(
        {
            src: __dirname + '/public',
            compile: compile
        }
    ));
app.use(express.static(__dirname + '/public'));


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

var port = 3131;
app.listen(port);
console.log('Listening on port ' + port + '...');

and when I'm trying to go in http://localhost:3131/ website stops responding in browser

This site can’t be reached

The connection was reset.

in nodemon it says that: enter image description here

If I remove morgan everything works OK. How can I solve that?

like image 219
gsiradze Avatar asked Apr 04 '16 08:04

gsiradze


2 Answers

just use app.use(morgan("dev)) and make sure you place your import of morgan at the top of your imports.. it will give error if you place it below the imports or for the atter the required dependencies..

like this:

import express from "express";
import morgan from "morgan";

import postRoutes from "./routes/postRoutes.js";

if you place your morgan import below the routes like this,

import express from "express";

import postRoutes from "./routes/postRoutes.js";
import morgan from "morgan";

it will give you a "Error: listen EADDRINUSE: address already in use"...

hope this helps anyone...

like image 140
Richard Rosario Avatar answered Oct 11 '22 17:10

Richard Rosario


The error logs shows "Morgan deprecated default format: use combined format".

It's quite simple, replace

app.use(logger);

with

app.use(logger('combined'));

like image 13
Ademola Adegbuyi Avatar answered Oct 11 '22 17:10

Ademola Adegbuyi