Node.js app.get not called

total node.js noobie, started playing with demo codes from different tutorials and websites, and I noticed something that I do not understand ...

namely, if I have index.html in my / shared folder, then

app.get("/", function (req, res) { console.log("get /"); res.redirect("/test.html"); }); 

just not called. As soon as I rename index.html to index2.html, the method is called and redirected to /public/test.html

this is what i have:

 var io = require('socket.io'), express = require('express'), MemoryStore = express.session.MemoryStore, app = express.createServer(), sessionStore = new MemoryStore(); app.configure(function () { app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ store: sessionStore, secret: 'secret', key: 'express.sid' })); app.use(express.static(__dirname + '/public')); }); 

and the rest is largely taken from this lesson: http://www.danielbaulig.de/socket-ioexpress/

The same problem occurs with any other file. If I have /public/test.html, then when I call

 http://localhost:8201/test.html 

this app.get is not called:

 app.get("/test.html", app.authenticateUser, function (req, res) { console.log("get /test.html"); res.redirect("/test2.html"); }); 

When I delete test.html, I go to test2.html ...

The reason I'm trying to redirect is because the user has not logged in. I do not want it to open index.html, but rather I wanted to forward it to login.html, which is not possible if index.html exists. The only "solution" is to make the client side suck, I do not want index.html to load clients in the browser just to forward it to login.html, the server must handle this in my oppinion.

+6
source share
3 answers

This is because express filters the request before it gets into your code. He finds the file and returns it to the browser.

The solution is to either send the event through socket.io, telling the code in the user's browser to redirect or move the file to personal space (outside the public directory) and transfer it through "fs", as CydGy suggested.

+1
source

The problem is that your static app.use(express.static(__dirname + '/public')) file app.use(express.static(__dirname + '/public')) is in front of your router. When you write

 app.configure(function () { app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ store: sessionStore, secret: 'secret', key: 'express.sid' })); app.use(express.static(__dirname + '/public')); }); 

it is equivalent

 app.configure(function () { app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ store: sessionStore, secret: 'secret', key: 'express.sid' })); app.use(express.static(__dirname + '/public')); app.use(app.router); //Express implicitly appends router middleware! }); 

because Express implicitly adds router middleware to the end of the stack unless you add it explicitly somewhere. Now you clearly see that static middleware is in front of the router. If a static file is found, it is served by app.use(express.static(__dirname + '/public')) , and the router app.use(app.router) never called. If you want your request to always go through the router, you should put it in front, for example

 app.configure(function () { app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ store: sessionStore, secret: 'secret', key: 'express.sid' })); app.use(app.router); //Now router comes first and will be executed before static middleware app.use(express.static(__dirname + '/public')); }); 
+16
source

this very good tutorial (http://www.danielbaulig.de/socket-ioexpress/) deals with sockets. And I think this is not useful for this case.

So take a look:

 app.get("/test.html", app.authenticateUser, function (req, res) { 

but where is the app.authenticateUser ? he undoubtedly blocks

So, replaces it with:

 app.get("/test.html", function (req, res) { 

or change your app.authenticateUser .

(And use the fs module to read your file, and then you can res.send(file); )

Remember to write .html in your URL, otherwise you must replace "/test.html" with "/test" )

Hope this helps you.

0
source

Source: https://habr.com/ru/post/916023/


All Articles