D
D
deadkEEper12015-09-06 14:34:40
Express.js
deadkEEper1, 2015-09-06 14:34:40

How to run 2 middlware for one request url?

var express = require('express');
var app = express();


var mw1 = function(req, res){
 res.send('Hello')
 next();


}


var mw2 = function(req, res){
 res.send('world')
}


app.get('/', mw1, mw2);


app.listen(3030)'


err is 'Can't set 2 headers in line.' So question is How can i response 'Hello world' using two MW's for one url request/ Thanks)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2015-09-06
@k12th

You shouldn't do res.send multiple times.

D
deadkEEper1, 2015-09-06
@deadkEEper1

Sorry, let's rephrase the question. The code should display the 'hello world' page with 2 functions. here is the code that outputs only 'hello', what needs to be fixed? Thank you, if you can show on my example);
PS Added next to mw1 argument

var express = require('express');
var app = express();


var mw1 = function(req, res, next){
 res.send('Hello')
 next('mw2');


}


var mw2 = function(req, res){
 res.send('world')
}


app.get('/', mw1, mw2);


app.listen(3030)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question