12

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?

Community
  • 1
  • 1
gsiradze
  • 4,583
  • 15
  • 64
  • 111

2 Answers2

13

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

It's quite simple, replace

app.use(logger);

with

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

Ademola Adegbuyi
  • 934
  • 1
  • 16
  • 25
2

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...