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:
If I remove morgan
everything works OK. How can I solve that?
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...
The error logs shows "Morgan deprecated default format: use combined format".
It's quite simple, replace
app.use(logger);
with
app.use(logger('combined'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With