First, a little background and context :
I am developing an application with nodejs
+ express
.
I am new to this technology, and the application will be of some complexity. So I'm doing it for modules , each on a specific route.
EDITO
After a little more trasteo , I'm 100%
that the bug is in how I use the routes; edito, to shorten it a bit and make it more concrete.
For an AJAX request to this URL
POST http://localhost:8080/auth/allowed
I have the following code (it's an extract, the real one is bigger, only what I think is relevant):
// main.js
const AuthModule = require( './auth' );
app.post( '/auth/*', AuthModule );
// Archivos estáticos. Sin autorización.
app.get( '*', EX.static( join( __dirname, 'static' ), {
dotfiles: 'ignore',
index: 'false',
redirect: false
} ) );
app.listen( 8080 );
Where all the modules load correctly.
AuthModule
Located in the subdirectory auth
, with 2 files:
// index.js
'use strict';
const EX = require( 'express' );
const AllowedModule = require( './allowed' );
const Router = EX.Router( );
Router.use( function( req, res, next ) { require( 'console' ).log( 'Módulo AUTH' ); next( ); } );
Router.post( '/allowed', AllowedModule );
module.exports = Router;
and
// allowed.js
'use strict';
function Allowed( req, res ) {
res.json( {
lastupdate: currTime,
users: [
[ 'Usuario número 1', 1 ],
[ 'Usuario número 2', 2 ],
]
} );
res.end( );
}
module.exports = Allowed;
Now, what happens: when I make a request for the file /login.html
, it performs an AJAX POST /auth/allowed
.
I have established dump points to console, which shows that I enter the file auth/index.js
. From that file, you should call the middleware auth/allowed.js
. However, in the browser I get a nice code 404
.
I understand that, for some reason, from the file auth/index.js
is not called to my middleware auth/allowed.js
; instead, we continue with the primary route table (the one I set in main.js
). The call is treated as a request for a static file, which static
tries to reply.
What I would expect is that auth/index.js
would be called allowed
, but that does not happen .
In the main.js
, I set the correct path as POST.
Within auth/index.js
, I also use a POST.
The route I hope to follow is
- POST
/auth
- >auth/index.js
- POST
/auth/allowed
- >auth/allowed.js
.
Well, I think that's it.