Node.js中间件阻止了该程序,为什么禁用该程序才能运行?

时间:2018-12-15 10:53:02

标签: node.js express

const express = require('express');
const fs = require('fs');

const app = express();

app.set('view engine', 'hbs');
app.use(express.static(__dirname + '/public'));

// MIDDLEWARE 2
app.use((req, res, next) => {
  res.render('test');
});

//MIDDLEWARE 3
app.use((req, res, next) => {
  let now = new Date().toString();
  let log = `At ${now} a peroson visited ${req.method} ${req.url}`;
  console.log(log);
  fs.appendFile('visitors.log', log + '\n', err =>{
    if(err) return err;
  });
  next();
});


app.get('/', (req, res) => {
  res.send(index.html);
});


app.get('/about', (req, res) => {
  res.render('about', {
    title: 'About Us'
  });
});




app.listen(3000, () => {
  console.log('Server is running on port 3000')
});

我期望我的中间件2阻止所有其他路由,但是我的其他路由运行正常。我的代码是否存在任何错误,或者我正在学习的内容不再有效?

1 个答案:

答案 0 :(得分:0)

res.render之后,您需要致电next()