It looks as if you're supposed to define all of your middlewhere before you start defining routes, as noted in this answer.
You're not explicitly using app.use(app.router)
, but it's automatically called when you use app.get
.
Knowing this, I would most likely change your code to something similar to this:
var express = require('express');
var request_counter = 0;
var app = express()
app.use(express.basicAuth('test', 'test'))
// count/log the requests for all except '/'
app.use(function(req, resp, next) {
if (req.path != '/') {
console.log('request# ' + (++request_counter));
}
next();
})
//serve the root (welcome)
app.get('/', function(req, resp, next) {
resp.end('welcome');
})
// serve "/pages"
app.post('/pages', function (req, resp, next) {
console.log('in the POST function');
resp.end('in the POST function');
})
// serve 404 for all the rest
app.all('*', (function (req, resp) {
resp
.status(404)
.end('BB: not found')
;
}))
app.listen(1234);