错误:Route.post()需要一个回调函数但得到一个[object String]

时间:2017-10-03 20:35:46

标签: javascript express post methods

我正在创建一个简单的Express应用程序,当我尝试路由POST方法时出现此错误:

Error: Route.post() requires a callback function but got a [object String]

我已将app.js拆分为多条路线,以下是相关文件:

app.js

var express = require('express');
var app = express();

var port = process.env.PORT || 5000;

var authRouter = require('./src/routes/authRoutes')();

app.use(express.static('public'));
app.use('/Auth', authRouter);
// app.set('views', path.join(__dirname, '/src/views');
app.get('/', (req, res) => {
  //Serve react index page
  res.sendFile('/index.html', {root: __dirname + '/src/views/'});
});
app.listen(port, (err) => {
  if (err){
    console.log('Error: ' + err);
    return;
  }
  console.log('Server running on port ' + port);
});

authRoutes.js

var express = require('express');
var authRouter = express.Router();

var router = () => {
  authRouter.route('/signIn')
      .post('/', (req, res) => {
          console.log('hello POST');
      });
  return authRouter;
}

module.exports = router;

index.html(摘录)

  <div class="row" style="max-width: 776px">
    <form action="/Auth/signIn" method="post" class="col s12">
      <div class="row">
        <div class="input-field col s6">
          <i class="material-icons prefix">account_circle</i>
          <input id="icon_prefix" type="text" class="validate">
          <label for="icon_prefix">First Name</label>
        </div>
        <div class="input-field col s6">
          <i class="material-icons prefix">lock</i>
          <input id="icon_telephone" type="password" class="validate">
          <label for="icon_telephone">Password</label>
        </div>
        <button class="btn waves-effect waves-light" type="submit" name="action" style="margin: 0 auto; display: block;">Submit
          <i class="material-icons right">send</i>
        </button>
      </div>
    </form>
  </div>

文件树

screenshot

我已经查到了这个问题,但我看到的解决方案似乎并没有解决我的具体问题。谢谢!

0 个答案:

没有答案