2

I am trying to learn middleware in express js. Could anyone help where I am missing? Here is my code

var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');

var app = express();

app.use("/", function(req, res, next){
  console.log("this is my second output");
  next();
});

app.get('/', function(req,res){
  console.log("this is my first output");
//res.send('Hello World');
});

app.listen(3000, function(){
  console.log('Server started on port 3000...');
})

I am getting Server started on port 3000.. when I run on cmd and getting "page is not working" on localhost:3000

Edited

I got

Server started on port 3000...
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output

after some time. But localhost:3000 is still not working

3 Answers 3

3

The reason you get the "page is not working" message is because your application does not respond to any request it receives.

You'll need to uncomment that res.send('Hello World'); in app.get('/', ...). After that your code works perfectly fine.

Note, however, that in the structure of your code, your middleware app.use(...) is called before you get to the main logic for the route (app.get(...)), contrary to what's indicated by your console.log calls.

1

enter image description here

   var express = require('express');
    var bodyParser = require('body-parser');
    var path = require('path');

    var app = express();
    // use this middleware to pass all the requests
    app.use("/", function(req, res, next){
      console.log("this is my second output");
    // move to next middleware
      next();
    });
    //handle all the get requests to localhost:3000 url
    app.get('/', function(req,res){
      console.log("this is my first output");
    // send the response
    res.send('Hello World');
    // or you can send the response like this 
    // res.json(JSON.stringify({"success":"true"}));
    });

    app.listen(3000, function(){
      console.log('Server started on port 3000...');
    })

send a get request to http://localhost:3000

0

Mainly, Middlewares execution consists of two things: ->

  1. next()
  2. app.use()
const express = require('express');
const app = express();

const hola = function(req, res, next) {
  console.log("are you okay?");
  next();
  console.log("Yes I'm okay....");
}
app.use(hola)

app.get('/',(req, res) => {
  res.send('HEY SUDEEPA WELCOME TO OUR SERVER !!!
})

app.listen(3000,() => {
  console.log('Server started at port 3000!!!! ');
})

In the above code if we do not mention the next() method in hola function then after clicking localhost:3000 our will got crashed because it is not getting it's next middleware which is our app.get. So, if we are not mentioning next() then it could be our one of reason for not getting executed Middlewares.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.