javascript - ExpressJs res.sendFile doesn't work after middleware - Stack Overflow

I'm trying to understand JWT and how they work with Node and Express .js. I have this middleware t

I'm trying to understand JWT and how they work with Node and Express .js. I have this middleware that tries to authenticate users with a token:

app.use(function(req, res, next) {
 if(req.headers.cookie) {
var autenticazione = req.headers.cookie.toString().substring(10)
autenticazione = autenticazione.substring(0, autenticazione.length - 3)
console.log(autenticazione)
jwt.verify(autenticazione, app.get('superSegreto'), function(err) {
  if (err) {
    res.send('authentication failed!')
  } else {
  // if authentication works!
    next() } })
   } else {
    console.log('errore')} })

And this is the code for my protected url:

app.get('/miao', function (req, res) {

res.sendFile(__dirname + '/pubblica/inserisciutente.html')
res.end() })

Even though the path is correct (I even tried with path.join(__dirname + '/pubblica/inserisciutente.html) and got the same result), when visiting the url I just get a blank page (with even node conde inside) I also set: app.use(express.static('/pubblica')) P.S. if I try to replace res.sendFile(..) with res.send('Some stuff') I can correctly view it on the page. What am I doing wrong?

I'm trying to understand JWT and how they work with Node and Express .js. I have this middleware that tries to authenticate users with a token:

app.use(function(req, res, next) {
 if(req.headers.cookie) {
var autenticazione = req.headers.cookie.toString().substring(10)
autenticazione = autenticazione.substring(0, autenticazione.length - 3)
console.log(autenticazione)
jwt.verify(autenticazione, app.get('superSegreto'), function(err) {
  if (err) {
    res.send('authentication failed!')
  } else {
  // if authentication works!
    next() } })
   } else {
    console.log('errore')} })

And this is the code for my protected url:

app.get('/miao', function (req, res) {

res.sendFile(__dirname + '/pubblica/inserisciutente.html')
res.end() })

Even though the path is correct (I even tried with path.join(__dirname + '/pubblica/inserisciutente.html) and got the same result), when visiting the url I just get a blank page (with even node conde inside) I also set: app.use(express.static('/pubblica')) P.S. if I try to replace res.sendFile(..) with res.send('Some stuff') I can correctly view it on the page. What am I doing wrong?

Share Improve this question asked Oct 18, 2015 at 18:49 skjorrfaceskjorrface 1612 silver badges17 bronze badges 1
  • Please indent your code properly. Very hard to follow code that is improperly indented. – jfriend00 Commented Oct 18, 2015 at 19:27
Add a ment  | 

2 Answers 2

Reset to default 13

res.sendFile() is asynchronous and it will end its own response if it is successful.

So, when you call res.end() right after you start res.sendFile() you are ending the response before the code has actually sent the file.

You can do it like this:

app.get('/miao', function (req, res) {

    res.sendFile(__dirname + '/pubblica/inserisciutente.html', function(err) {
        if (err) {
            res.status(err.status).end();
        }
    });
});

See the Express doc for res.sendFile() here.

if you want to end the response with res.end() then you must not mention or specify it after res.sendFile() because res.sendFile() is an asynchronous function that means it will take some time to execute and in that meantime next instruction which is in your case is res.end() will execute and that's why you didn't see any response send by the res.sendFile

You can visit the documentation to know more about res.sendFile() visit documentation

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743726045a4496631.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信